#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171

    Getter: Accessign private property with __get()


    __get() is utilized for reading data from inaccessible properties
    1 - Some properties must be private. Please show a simple real life example. Not a car example.

    2 - If some properties "must be inaccessible", why bother with __get and retrieve their value? They are supposed to be inaccessible!

    What is the point of these:
    PHP Code:
    class test
            
    {
                private 
    $var "Can't touch this";
            }
        
    $object = new test();
        echo 
    $object->$var;

    //Fatal error: Cannot access empty property 
    __get($arg) allows accessing the value of a private property or any other value we wish when a private property is tried to be access from outside of a class. Which of these codes below is used in real life?
    PHP Code:
    class test
            
    {
                private 
    $var "Can't touch this";
                public function 
    __get($arg)
                    {
                        return 
    $this->var;
                    }
            }
        
    $object = new test();
        echo 
    $object->$var;

    //Can't touch this 
    Or
    PHP Code:
    class test
            
    {
                private 
    $var "Can't touch this";
                public function 
    __get($arg)
                    {
                        return 
    "Can't access a private property";
                    }
            }
        
    $object = new test();
        echo 
    $object->$var;    

    //Can't access a private property 
    Or
    PHP Code:
    class test
            
    {
                private 
    $var "Can't touch this";
                public function 
    get_private()
                    {
                        return 
    $this->var;
                    }
            }
        
    $object = new test();
        echo 
    $object->get_private(); 

    //Can't touch this 
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    1. There's a difference between "inaccessible" and "unmodifiable".
    PHP Code:
    class user_database_model {

        public 
    $id;
        public 
    $name;
        public 
    $email;
        public 
    $pendingEmail;
        public 
    $pendingEmailCode;


    PHP Code:
    /**
     * @property-read int $id
     * @property-read string $name
     * @property-read string $email
     */
    class user {

        private 
    $model;

        public function 
    __get($name) {
            return 
    $this->model->$name;
        }

        public function 
    changeEmail($email) {
            if (
    $email == $this->model->email) {
                return 
    false;
            } else if (
    $email != $this->model->pendingEmail) {
                
    $this->model->pendingEmail $email;
                
    $this->model->pendingEmailCode substr(md5(uniqid()), 06);
            }
            return 
    $this->model->pendingEmailCode;
        }


    (more-or-less real world example that I did a couple days ago)

    There's a model for the user and a class for the user. The model gives access to a bunch of things that the class may be interested in. Outside code is probably interested in the id, name, and email, but shouldn't be able to set them, so there's __get to allow access to the underlying model. Meanwhile the outside code may want to set the pending email address, but that entails additional work so there's a special method for it.

    Could outside code do ->id = 123 and screw up future ->id references? Yes, but I give enough credit to my coworkers to not do something stupid like that. Could it do ->pendingEmail and get what's in the database model? Yes, but that doesn't worry me. Also helps that everything we do gets code-reviewed by someone else.

    2. Should be using ->var.
    #1 Can't access $var because it's private. Will cause an error. I don't think there's any "point" to it
    #2 test provides read-only access to $var
    #3 test makes calling code think the value of $var is that one string. Don't do that. Get rid of __get and let PHP warn about it
    #4 The alternative to __get. Perfectly acceptable
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Keeping all these quesitons under 1 thread.

    Just wondering why this doesn't print 5435, instead it prints 543!
    PHP Code:
    class calc
        
    {
            private 
    $variableOne 5;
            
            public function 
    change()
                {
                    
    $this->variableOne 43;
                }
            public function 
    show()
                {
                    return 
    $this->variableOne;
                }
        }
    $object = new calc();
    echo 
    $object->show();
    echo 
    $object->change();
    echo 
    $object->show(); 
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,952
    Rep Power
    4033
    It shows 543 because your first call to show() returns and echos 5, and your second call to show() returns and echos 43. The call to change() returns nothing, and thus echos nothing.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo