TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Getting classname where function was called from (http://www.talkphp.com/advanced-php-programming/5448-getting-classname-where-function-called.html)

Tanax 06-14-2010 03:44 PM

Getting classname where function was called from
 
How would I get the classname of a class where I called another class's function?

one.php
PHP Code:

class One
{

     public static function 
test()
     {

          echo 
Two::test();

     }



two.php
PHP Code:

class Two
{

     public static function 
test()
     {

         return 
'You called this from: ' $something;

     }



Calling One::test() would echo out:
You called this from: One, or
You called this from: one.php, or
You called this from: path/to/file/one.php

Is that possible??

Enfernikus 06-14-2010 08:16 PM

php Code:
echo get_class($this);
echo __FILE__;

Salathe 06-14-2010 08:52 PM

If I'm understanding what you want, it's not as simple as Enfernikus makes out. However, it would be possible: parsing the contents of debug_backtrace might be your best bet.

Enfernikus 06-14-2010 10:11 PM

I misunderstood his request, side effect of trying to aid when at work in the midst of a coding frenzy. There's no easy method of doing what you'd like but as Salathe said, debug_backtrace would most likely be your best option.

Tanax 06-16-2010 02:44 PM

Didn't work quite as I wanted/expected, but thanks for the help!

Salathe 06-16-2010 05:52 PM

So, have you got the issue solved? Have any more questions? It's nice being thanked, but the thread has been kind of left hanging. :-P

Tanax 06-17-2010 08:13 PM

Haha, it's solved for the moment but not the way I wanted.
I have an HMVC framework with modules, I wanted so when I load a view, it will first check if the view exists in the current module - IF the view was loaded from a module - and then if not found there check the regular application/views/ folder for the view-file.

Kinda like this:
modules/welcome/controllers/welcome.php
PHP Code:

View::factory('website/index'); 

That will check modules/welcome/views/website/index.php, if found, render it.
If not found, check application/views/website/index.php, if found, render it.
If not found, check all other modules views directories, if found, render it.
If not found, generate error message.

On the other hand, if I do like this:
application/controllers/welcome.php
PHP Code:

View::factory('website/index'); 

It will check application/views/website/index.php, if found, render it.
If not found, check all modules views directories, if found, render it.
If not found, generate error message.

All of that is quite simple, takes some work but I can make that work.
The thing that complicates it is that I want to be able to load a view from within a view.

Like so:
modules/welcome/views/website/index.php
PHP Code:

<?php echo View::factory('footer'); ?>

It will check modules/welcome/views/footer.php, if found, render it.
If not found, check application/views/footer.php, if found, render it.
If not found, check all other modules views directories, if found, render it.
If not found, generate error message.

I think you get the point..
So somehow I need to check if the factory() call came from a class, if so, check which class, get the name, get the location. If not from a class - but from a viewfile - get the location of that viewfile.


All times are GMT. The time now is 04:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0