Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Can't you just set static member of autoload class during startup containing all necessary information?</p> <p>debug_backtrace() cannot be reliable source of information. What if somebody would like to use your libraries <strong>and</strong> your autoloader, but without one of the starting layers? Would it be possible that way?</p> <p>All data used by class/function should be placed within that class or as parameter for the function. Because autoloader can be any callback, you can do something like this:</p> <pre><code>class FrameworkAutoloader { public $appName; public $path; public function setAppName($name) { $this-&gt;appName = $name; } public function setPath($path) { $this-&gt;path= $path; } function __autoload($className) { $path = $this-&gt;path. DS . 'applications' . DS . $this-&gt;appName . DS . 'models' . DS . 'class.' . strtolower($className) . '.php'; if (file_exists($path)) { require_once($path); } else { throw new Exception('Can\'t find a model at "' . $path . '".'); } } } $autoloader = new FrameworkAutoloader(); $autoloader-&gt;setAppName('asd'); //you can also apply those within constructor, but leave setters $autoloader-&gt;setPath('asd'); spl_autoload_register(array($autoloader, '__autoload')); </code></pre> <p>That's all. You will be able to set path and appname dynamically - just by changing object's variables using setters. </p> <p>Why we should do that this way? In this code there is no "magic" going around. You can write documentation using PHPDOC to every function and user will know where all params come from. Another advantage is that I can use this code anywhere, I don't need to know, that the class uses Dispatcher::getApplicationName(). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload