SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __autoload question

    I never worked much with PHP 5 before, but now I have a client who can provide a server where PHP 5 is installed.

    I was just wondering about the __autoload magic function, what happens if you have two classes in one file, like this:

    PHP Code:
    class Foo
    {
      function 
    __construct ()
      {
      }
      function 
    doStuff ()
      {
        return new 
    Bar ();
      }
    }
    class 
    Bar
    {
      public 
    $message;
      function 
    __construct ()
      {
        
    $this->message 'Hello World';
      }

    What if the file's name is 'Foo.php' and you instantiate them like this:

    PHP Code:
    $f = new Foo ();
    $bar $f->doStuff (); 
    Foo would be loaded correctly (when your __autoload function is similar to the one on http://www.php.net/__autoload), but what happens to Bar?

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the statement new Foo(); is interpreted, __autoload() will be called. If you implement your __autoload() to load the file Foo.php, then this file will be loaded. Since Foo.php contains two classes, they will both be loaded at that time.
    Later, when you call doStuff(), and this calls new Bar(), the class Bar is already loaded, so no autoloading will occur.

  3. #3
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    When the statement new Foo(); is interpreted, __autoload() will be called. If you implement your __autoload() to load the file Foo.php, then this file will be loaded. Since Foo.php contains two classes, they will both be loaded at that time.
    Later, when you call doStuff(), and this calls new Bar(), the class Bar is already loaded, so no autoloading will occur.
    Ah that's good to know
    Thanks !


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
  •