Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to autoload a model class in specific folder HMVC
    text
    copied!<p>I'm currently writing my own PHP framework as a learning exercise using the HMVC design pattern. It all works :), but I've read many times that it's a bad habit to refer to static classes in your PHP code, which is exactly what I'm doing in my autoload function:</p> <pre><code>function __autoload($className) { $path = SERVER_ROOT . DS . 'applications' . DS . Dispatcher::getApplicationName() . 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 . '".'); } } </code></pre> <p>As you can see I get the current application using the static call <code>Dispatcher::getApplicationName()</code>, which is bad according to many people since it introduces dependencies. I can also get the applicationName using <code>debug_backtrace()</code>, since the class that initiates the model contains the ApplicationName as a property. Is that better, or are there other alternatives I haven't thought of?</p> <p>Thanks!</p> <p>Edit: forgot to mention that there's another problem with the above code: the controller's application does not always equal the dispatcher's application, since I'm using the HMVC design pattern (so controllers are called inside controllers). This can only be fixed using <code>debug_backtrace</code>.</p> <p>Edit: Instead of <code>Dispatcher::getApplicationName()</code> I now use <code>Request::getCurrentApplicationName()</code>. It now works again, because my request class saves all applications. Is this better, or is there a better way?</p> <pre><code>&lt;?php class Request { private static $_controllers = array(); private static $_applicationsNames = array(); public static function _getCurrentApplicationName() { return end(self::$_applicationsNames); } public static function _load($applicationName, $controllerName, $methodName) { // Add the application the the array (for autoloading). self::$_applicationsNames[] = $applicationName; // Check if the controller has already been instantiated. if (!isset(self::$_controllers[$applicationName . DS . $controllerName])) { require_once(APPLICATIONS_ROOT . DS . $applicationName . DS . 'controllers' . DS . 'class.' . $controllerName . '.php'); self::$_controllers[$applicationName . DS . $controllerName] = new $controllerName($applicationName); } // Get the user arguments. $arguments = array_slice(func_get_args(), 3); // Call the method. $result = call_user_func_array(array(self::$_controllers[$applicationName . DS . $controllerName], $methodName), $arguments); // Remove the last value from the applications array. array_pop(self::$_applicationsNames); } } </code></pre>
 

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