SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Autoload()

    Long story short: I'm working on redesigning an existing application, with a full rewrite. As a result, I'm forcing myself to stick to strict OOP, and something of a light MVC pattern as well. Since requiring individual files is tedious, I'm looking to integrate some sort of autoload function, using spl_autoload_register().

    Code:

    PHP Code:
    class Controller extends SingletonObject {
        private 
    $registry;
        private 
    $model;
        private 
    $view;
        private 
    $instance;

        private function 
    __construct(){
            
    spl_autoload_register(array($this"myAutoLoadFunction"));
        }

        public static function 
    singleton(){
            if(!(isset(
    self::$instance))){
                
    $c __CLASS__;
                
    $class = new $c();
                
    self::$instance $class;
            }
            return 
    self::$instance;
        }

        public static function 
    myAutoLoader($name){
            
    define(CLASS_PATH"/var/www/html/portal/dev/");
            try {
                if(
    is_readable(CLASS_PATH "/classes/" $name ".php")) {
                    require_once(
    CLASS_PATH "/classes/" $name ".php");
                } elseif(
    is_readable(CLASS_PATH "/interfaces/" $name ".php")) {
                    require_once(
    CLASS_PATH "/interfaces/" $name ".php");
                } elseif(
    is_readable(CLASS_PATH "/pages/" $name ".php")) {
                    require_once(
    CLASS_PATH "/pages/" $name ".php");
                } else {
                    throw new 
    Exception("The file you are attempting to access does not exist.");
                }
            } catch (
    Exception $exc) {
                echo 
    $exc->getTraceAsString();
            }
        }


    Unfortunately, I keep getting the following error wherever I try to instantiate the controller:

    PHP Code:
    Fatal errorUncaught exception 'LogicException' with message 'Passed array does not specify an existing static method' in /var/www/html/portal/dev/classes/Controller.php:15 Stack trace#0 /var/www/html/portal/dev/classes/Controller.php(15): spl_autoload_register(Array) #1 /var/www/html/portal/dev/classes/Controller.php(21): Controller->__construct() #2 /var/www/html/portal/dev/classes/Session.php(82): Controller::singleton() #3 /var/www/html/portal/dev/pages/TestSession.php(7): Session->dumpSession() #4 {main} thrown in /var/www/html/portal/dev/classes/Controller.php on line 15 
    Does anyone have any suggestions on registering an autoload function that doesn't require sticking a function floating out in a file somewhere? This one is stumping me.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I'm not entierly sure what you're trying to attempt but here's a snippet.
    PHP Code:
    <?php
    class Application_Autoloader
    {
        public static function 
    Register(){
            return 
    spl_autoload_register(array($this'Load'));
        }
        
        public static function 
    Load($object_name){
            
    #resolve path
            #does file exist
            #require_once
        
    }
    }

    Application_Autoloader::Register();
    Bear in mind too, that prior to 5.3, you cannot throw an exception from a registered autoload implementation.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I was trying to achieve was declaring the autoloader inside a class constructor.

    The issue I was running into was a simple one: I named the function myAutoLoader, and called myAutoLoadFunction.

    After correctly renaming the function, I was able to set the autoload inside the constructor for Controller (which is designed as the entry point for every page in the application, so it should always be loaded). So, what you posted above is more or less accurate, I just wanted to make it something that happened without me thinking about it during the normal course of a day.

    The information about not being able to throw an exception is useful. Would the correct course of action there be to raise a fatal error, then?

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes as $this refers to the current instance of the object
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •