1

Possible Duplicate:
php: determine where function was called from

I have an autoloading function my_function($class) that could be called by any script. It is called by PHP's spl_autoload_call() and that can happen everywhere. How do I script my_function($class) to detect when it is called by a specific $file?

Something like:

$file = '/htdocs/project/whatever.php';
if ($caller_file == $file) {
    ...

How do I find $caller_file? I do understand this question is confusing but I can't put it in better words right now. So if you have any problem understanding just let me know in the comments.

Edit (Context) I want to make the script load a different class's file if the file the autoloader would load is the same in which the autoloader is called.

For example giving:

namespace Something;
class Random extends Random {}

should not load itself, but a different file in which we have:

namespace Something;
class Random { ... }

In other words:

What if I'm defining MyName\Class in a/class.php and I want to extends itself with MyName\Class extends \MyName\Class (inside a/class.php) but I want the autoloader to look \MyName\Class in b/class.php when it is asked within a/class.php?

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • @hakre, I tried to backtrace the script below, but `random.php` (Random file) never comes out in the backtrace when calling itself. – Shoe Apr 26 '12 at 12:04
  • Where is your code in the question that makes use of backtrace? I don't see it. – hakre Apr 26 '12 at 12:06
  • I did a `var_dump()` in the autoloader script of `$debug` which comes out of `$debug = debug_backtrace()`. There's not the caller file I want because when he reach `a/random.php` the autoloader ask for `a/random.php` again (because of `Random extends Random`) causing the PHP "class not found" error. Instead I want the autoloader script to load `b/random.php` when `Random` is called within `a/random.php`. Do you get it? – Shoe Apr 26 '12 at 12:09
  • The function(s) registered via `spl_autoload_register()` is/are never called if a class of the particular name is already defined within the specified namespace. – Matthemattics Apr 26 '12 at 12:09
  • @Jeff: No I don't understand. The autoloader function get's a classname - not a file-name. So no idea why you talk about the autoloader reaches a file-name. And the code is still missing in your question, so it's absolutely not possible for me to decipher your problem. – hakre Apr 26 '12 at 12:11
  • @Lübnah, what if I'm defining `MyName\Class` in `a/class.php` and I want to extends itself with `MyName\Class extends \MyName\Class` inside `a/class.php` but I want the autoloader to look `\MyName\Class` in `b/class.php` when it is asked within `a/class.php`? – Shoe Apr 26 '12 at 12:12
  • @hakre, Yeah, you use the autoloader to include a file which contains the class. You know? It's the purpose of the autoloader to load the class... anyway, my autoloader obliviously convert the class name into a path to a file. As far as you need to know the autoloader function is `function my_autoloader($class) { $class = str_replace('\\', '/', $class); include(ROOT."a/$class.php"); }`. – Shoe Apr 26 '12 at 12:14
  • @Jeff: I know what an autoloader is and use spl to manage those often. So what is your actual question? Your autoloader is actually pretty simple (and prone to attacks). – hakre Apr 26 '12 at 12:15
  • @hakre, please, read my last reply to Lubnah comment. – Shoe Apr 26 '12 at 12:16
  • @JeffPigarelli: Assuming they're referred to in the global namespace, `MyName\Class` and `\MyName\Class` are in the same namespace. See: http://www.php.net/manual/en/language.namespaces.basics.php – Matthemattics Apr 26 '12 at 12:35
  • @Lübnah, yep, that's the point. – Shoe Apr 26 '12 at 13:03
  • @JeffPigarelli: Well then you're going to get a fatal, b/c you can't redefine a class that's already been defined, and you certainly can't make a class extend itself. Your autoload function won't even come into the picture in your cited example. – Matthemattics Apr 26 '12 at 13:07

1 Answers1

0

Have you tried debug_backtrace() ?

AO_
  • 2,573
  • 3
  • 30
  • 31