SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OO: moving from PHP4 to PHP5. A few (sometimes philosophical) questions.

    Hey,
    Finally fully moving from PHP4 to PHP5.

    I will keep my questions in this thread.

    Question 1: Should the __autoload() magic method be always used instead of includes files? If yes, where to put this method, inside a class I guess, otherwise it would be called a function. Or is it an utility function?

    Question 2: References now. PHP4 got it very confusing. If I am not mistaking, you don't need to pass by reference any more in PHP5. Then Why would you use references?

    Question 3: An abstract class is a class that is never instantiated but who holds methods that are common to child classes. Is that an obligation that the child class uses all the methods defined in the parent abstract class or can they use just one (in case of many child classes)? In my understanding, the abstract class is like a container for methods shared by other classes.

    Question4: Interfaces force classes to implement the methods they create (only public methods that is). My question is: when is that useful? If a class needs a public method, I will create it, otherwise the code wouldn't work. Then why force myself to implemant it? Are people in the custom of not creating methods that are necessary so that they must force themselves to do so through interfaces?

    Feel free to answer one of them, all of them, none of them.


  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    Hey,
    Question 1: Should the __autoload() magic method be always used instead of includes files? If yes, where to put this method, inside a class I guess, otherwise it would be called a function. Or is it an utility function?
    There can only be one __autoload() function at any given time, and they are never class methods. This one __autoload() function is responsible for the mapping and inclusion of all class files. Whether or not you decide to use it is a personal choice. SPL also has an autoload feature, but I don't know too much about it.

    Quote Originally Posted by jjshell View Post
    Question 2: References now. PHP4 got it very confusing. If I am not mistaking, you don't need to pass by reference any more in PHP5. Then Why would you use references?
    You could still use references for other types.

    Quote Originally Posted by jjshell View Post
    Question 3: An abstract class is a class that is never instantiated but who holds methods that are common to child classes. Is that an obligation that the child class uses all the methods defined in the parent abstract class or can they use just one (in case of many child classes)? In my understanding, the abstract class is like a container for methods shared by other classes.

    Question4: Interfaces force classes to implement the methods they create (only public methods that is). My question is: when is that useful? If a class needs a public method, I will create it, otherwise the code wouldn't work. Then why force myself to implemant it? Are people in the custom of not creating methods that are necessary so that they must force themselves to do so through interfaces?
    An abstract class is pretty much a mixture of a class and an interface. When extending it, you must implement any of the methods that are required. After that, the rules of extending from an abstract class are the same as extending from any other class. As for interfaces usefulness, this is rather subjective. While they do enforce compliance and provide clear documentation, their usage is much more understandable in statically-typed langauges with single-inheritance, such as Java. In PHP, you can rely on duck-typing, as you describe in your Q4.

  3. #3
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ryan Wray View Post
    SPL also has an autoload feature, but I don't know too much about it.
    If you're going to use autoload, I would highly recommend using the SPL autoload as it allows you to register multiple autoload functions. See spl_autoload_register
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    South Dakota
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    Question4: Interfaces force classes to implement the methods they create (only public methods that is). My question is: when is that useful? If a class needs a public method, I will create it, otherwise the code wouldn't work. Then why force myself to implemant it? Are people in the custom of not creating methods that are necessary so that they must force themselves to do so through interfaces?
    Look below at the simple classes for an example.

    PHP Code:
    interface iUser
    {
        public function 
    GetUser($someVar);
    }
     
    class 
    RestrictedUser implements iUser
    {
        protected 
    $userVar 'Joe';
     
        public function 
    GetUser($someVar)
        {
            
    // Who cares what $someVar is
            // We always return 'Joe'
            
    return $this->userVar;
        }
    }
     
    class 
    AdminUser implements iUser
    {
        public function 
    GetUser($someVar)
        {
            
    // Some code to get user information from another object.
            // return the other object
        
    {
    }
     
    class 
    SomeOtherClass
    {
        protected 
    $classUser;
     
        public function 
    __construct(iUser $someUser)
        {
            
    $this->classUser $someUser;
        }
     
        public function 
    WhoIsUser($userVar)
        {
            return 
    $this->classUser->GetUser($someUser);
        }
    }
     
    $adminUser      = new AdminUser;
    $restrictedUser = new RestrictedUser;
     
    $myFirstClass     = new SomeOtherClass($adminUser);
    $mySecondClass = new SomeOtherClass($restictedUser);
     
    $restrictedWhoIs $myFirstClass->WhoIsUser(63);
    $adminWhoIs     $mySecondClass->WhoIsUser(63);
     
    // Outputs 'Joe'
    echo $restrictedWhoIs;
     
    // Outputs whatever the class RestrictedUser sends back
    echo $adminWhoIs
    Of course there would be error handling and such in these. This is just to show where interfaces come in handy. The class SomeOtherClass has a function called WhoIsUser that sets a variable on initialization to the interface iUser. Because both RestrictedUser and AdminUser implement that interface, they are of type iUser that SomeOtherClass expects.

    Notice that they both have the GetUser method, but that they implement it in a different way. SomeOtherClass doesn't care how it's implemented, just as long as it is able to call the method and get a result.

    Can you see where you might now implement an interface? Of course this would work with abstract classes as well.

    Ted
    TDDJournal: An Experiment in Test Driven Development.

  5. #5
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    Question 2: References now. PHP4 got it very confusing. If I am not mistaking, you don't need to pass by reference any more in PHP5. Then Why would you use references?
    As the manual points out, they're not references, they're symbol table aliases. Perl has the same feature and calls them aliases, but they're not terribly useful in my experience. If you have a large data structure that's not object-oriented, they might be useful for performance reasons the same way as in PHP 4. Or you could use them as shorthand in some cases.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  6. #6
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding question 1, is that still a good coding practice to keep an "include.filename.php" file instead of using the autolad function (why is it called a method if doen'st exist in a class btw?)

    @Dagfinn: I would be grateful if you could post an example of what you just explained.

  7. #7
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // Imagine that this is part of a large non-OO data structure:
    $struct = array('documents' => array('document1' => array('image' => $image)));
    // Instead of copying all that data, we make an alias (in PHP 5, we don't need to do that with objects any more:
    $alias = &$struct;
    // Shortcut: make $short an alias for the image:
    $short = &$struct['documents']['document1']['image'];
    // Now if we change $short, it will be reflected in $struct:
    $short $otherimage
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  8. #8
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah I see. Thank you very much. Got it

    But in terms of OO, you don't have to "pass by reference" anymore, correct?

    Thanks to all of you for your patience!


  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    But in terms of OO, you don't have to "pass by reference" anymore, correct?
    No, when you are passing objects around in PHP5 you are passing a reference to the object in question. Copies are not made unless manually done so.

  10. #10
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for includes files, is that ok?

    Question 5: I must honestly say that the concept of overload and its use is beyond me. I need some explanations from scratch I guess.

  11. #11
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, overloading is:
    Quote Originally Posted by www.it.bton.ac.uk/staff/je/adacraft/glossary.htm
    Giving multiple meanings to the same name, but making them distinguishable by context
    So, for example, when you define a __set() magic method you are overloading the = operator for properties. For example:

    PHP Code:
    <?php
    class Example
    {
        private 
    $attributes = array();

        public function 
    __set($property$value)
        {
            
    $attributes[$property] = $value;
        }

        public function 
    __get($property)
        {
            return 
    $attributes[$property];
        }
    }
    $example = new Example();
    $example->anyProperty "Hello";
    ?>
    To the user, it just looks like they are setting a property using standard assignment, however we are using an associative array to keep track of the variables in question.

    Not a particulary helpful example, but hopefully serves to show how overloading works in PHP5.

  12. #12
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's worth noting here that what the PHP5 manual calls overloading really isn't. __get, __set and __call are missing property handlers, nothing else. You can do some nifty voodoo with them (including emulation of overloading), but I'd prefer everyone call them for what they are.

    This is an example of overloading (not really doable in PHP):
    PHP Code:
    class Foobar {
       public function 
    execute(Foo $foo) { $foo->doFoo(); }
       public function 
    execute(Bar $bar) { $bar->doBar(); }

    If you now reread the quote, the meaning is more clear:
    Giving multiple meanings to the same name, but making them distinguishable by context
    Name is what you call the function, context is what you pass in. Compare with operator overloading.

  13. #13
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku View Post
    I think it's worth noting here that what the PHP5 manual calls overloading really isn't.
    They used to use the term overloadable, which is technically true, but they seem to have changed that.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  14. #14
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When do you guys typically use overloading. It still doesn't talk to me much...

  15. #15
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    When do you guys typically use overloading. It still doesn't talk to me much...
    It can be quite useful for metaprogramming, for example. It can also help with getters/setters, and other features. You probably won't have many uses for it, but every so often they just make things handy.


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
  •