SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How does _autoload works?

    Hi:

    i know there are many threads and I read documentation on PHP site, but I am still not clear.

    - Is this function used in class or independent?
    - Should it be used in every file?

    i know there are many examples but I have trouble understanding it? Do I have to name my files specifically? Like with the classname being included?

    Thanks

    EDIT: Sorry, wrong forum (I think). If so, please move this to PHP
    ---------------------------
    Errors = Improved Programming.
    My Site

  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)
    It goes along the lines of....
    PHP Code:
    <?php
    //--> define the autoload function
    function __autoload$class )
    {
        
    //add whatever logic you require to include class definition
        
    require_once(sprintf('/classes/%s.php',$class));
        return;
    }

    //--> create an object, which autoload will try to find and require for you.
    $oObject = new Object();
    ?>
    @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
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The autoload function is defined just like a normal function. Like SilverB's example above, you have a function called __autoload.

    This takes just one parameter, the class name. So if I instantiated a class like:
    PHP Code:
    $myObject = new RubberChicken(); 
    It would first check if the class RubberChicken has already been loaded. If not, it goes to the autoload function. It sends the class name to the autoload function, and then attempts to load the RubberChicken class again - if it still can't be found, it errors.

    IIRC The same works with interfaces, and extentions - for example:
    PHP Code:
    class RubberChicken implements Useless{ } 
    A call to the RubberChicken class would (if neither had already been loaded) run the autoload function with both RubberChicken and Useless - and in this example:
    PHP Code:
    class RubberChicken extends FakePoultry 
    It would run the autoload function with both RubberChicken and FakePoultry.

    __AutoLoad saves both you and the server time. You, because you don't have to edit the file loading all the class files, and the server because it doesn't need to load classes it won't use.

    Win/Win.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, I only need one instance of this function? Let's say in my init.php file, which will be called from every frontpage, right?

    I am assuming that my class and file name should be same as it only takes one parameter.

    What about the path from where it looks for those files? Can I use multiple paths so that it can look for file in different directories? Does looking in different directories (all the time) puts more load on the system?
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana View Post
    What about the path from where it looks for those files? Can I use multiple paths so that it can look for file in different directories? Does looking in different directories (all the time) puts more load on the system?
    You can use __autoload() in conjunction with setting your include path.

    PHP Code:
    $paths implode(PATH_SEPARATOR, array(
      
    'app',
      
    'app/inc',
      
    'app/lib'
    ));
    set_include_path($paths);
    function 
    __autoload($class)
    {
      
    $paths explode(PATH_SEPARATORget_include_path());
      foreach(
    $paths as $path)
        if(
    file_exists($path.'/'.$class.'.php'))
          require_once(
    $path.'/'.$class.'.php');

    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That clears it.
    Much appreciated
    ---------------------------
    Errors = Improved Programming.
    My Site


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
  •