Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:23:46+00:00 2026-05-24T08:23:46+00:00

I’m currently writing my own PHP framework as a learning exercise using the HMVC

  • 0

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:

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 . '".');
    }
}

As you can see I get the current application using the static call Dispatcher::getApplicationName(), which is bad according to many people since it introduces dependencies. I can also get the applicationName using debug_backtrace(), 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?

Thanks!

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 debug_backtrace.

Edit: Instead of Dispatcher::getApplicationName() I now use Request::getCurrentApplicationName(). It now works again, because my request class saves all applications. Is this better, or is there a better way?

<?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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:23:47+00:00Added an answer on May 24, 2026 at 8:23 am

    Can’t you just set static member of autoload class during startup containing all necessary information?

    debug_backtrace() cannot be reliable source of information. What if somebody would like to use your libraries and your autoloader, but without one of the starting layers? Would it be possible that way?

    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:

    class FrameworkAutoloader
    {
        public $appName;
        public $path;
    
        public function setAppName($name) { $this->appName = $name; }
        public function setPath($path) { $this->path= $path; }
    
    
        function __autoload($className) {
            $path = $this->path. DS . 'applications' . DS . $this->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->setAppName('asd'); //you can also apply those within constructor, but leave setters
    $autoloader->setPath('asd');
    spl_autoload_register(array($autoloader, '__autoload'));
    

    That’s all. You will be able to set path and appname dynamically – just by changing object’s variables using setters.

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.