#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    73
    Rep Power
    36

    Tutorial on Class function visibility (that public/protected/private dilemma)


    You can check out it here at flying beetroot

    Understanding OO Visibility - PHP

    Enjoy!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Your tutorial is well written and does a good job of explaining those concepts.

    Just because I'm OCD and my life revolves around PHP I'm going to point out two things:

    (1) A constructor actually doesn't need to be public. In fact, there is a particular use-case where you would not want the constructor to be public:
    PHP Code:
        class special_class
        
    {
            public static function 
    factory_method()
            {
                return new 
    self;
            }
            protected function 
    __construct()
            {
                echo 
    "constructed";
            }
        }
        
    $inst special_class::factory_method(); 
    Declaring the constructor as protected in this case prevents me from bypassing the factory method.

    (2) You can actually call a protected or private method from an instantiation inside the class that contains the functions. In fact, the instantiation doesn't even need to be inside the class containing the functions. A protected or private method can be called on any object from any context that has visibility of those methods. Meaning that even something like this is legal syntax:
    PHP Code:
        class special_class_a
        
    {
            public function 
    pub($arg)
            {
                
    $arg->pro();
            }
            protected function 
    pro()
            {
                echo 
    "pro-a";
            }
        }
        
        class 
    special_class_b extends special_class_a
        
    {
            protected function 
    pro()
            {
                echo 
    "pro-b";
            }
        }
        
        
    $inst1 = new special_class_a;
        
    $inst2 = new special_class_b;
        
        
    $inst1->pub($inst2); 

    Comments on this post

    • FlyingBeetroot agrees : Thanks mate - I'll update accordingly :)
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    73
    Rep Power
    36
    Thanks mate - I did know about the factory idea; but all the code I've seen uses die() in the public constructor to prevent standard instantiation.

    As for point 2 - valid argument (in fact I wrote a router class for URL parsing that does this) - but it's slightly different in nature to the example I gave. Nonetheless; given it is around the same lines; I will update my tutorial to include this exception.

    Cheers for taking the time to read it though Greatly appreciated.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    73
    Rep Power
    36
    OK, it's updated and I think with your comments incorporated it's a lot better. Thanks again.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    As for point 2 - valid argument (in fact I wrote a router class for URL parsing that does this) - but it's slightly different in nature to the example I gave. Nonetheless; given it is around the same lines; I will update my tutorial to include this exception.
    Ah, actually my example using extension was meant to show a "most complicated" situation. It works with other cases too, such as passing an instance of special_class_a to pub() or constructing an instance of "self" inside pub instead of using the argument. At least under PHP 5.3, the tutorial's example of self instantiation executes correctly with no errors.

    However, this is sort of an edge case as objects are rarely used in this way.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo