Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__get() function behavior in php

I was trying to find the sequence in which magical methods are called in PHP. Hence wrote a very basic program

class testme
{
    public $var1;
    /*function __construct()
    {
        echo'<br/> Constructor called';
    }*/
      public function __set($name, $value)
    {
        echo'<br/> You are in sssset function';
    }
    public function __call($method,$arg)
    {
        echo '<br/> call method';
    }
    public function __get($name)
    {
        echo'<br/> You are in get function';
    }
    public function __isset($name)
    {
        echo'<br/> You are in isset function';
    }
    public function __unset($name)
    {
        echo'<br/> You are in unset function';
    }   
       function __destruct() {
       print "<br/>Destroying " . $this->name . "\n";
   }
}
$obj = new testme;
$obj->var1=5;

The expected output was

You are in set function
Destroying 

Getting:

You are in get function
Destroying 

$obj->var1=5 Here I am setting the value to the class var then why it is calling __get. What is wrong here?

like image 824
user269867 Avatar asked Apr 25 '11 12:04

user269867


People also ask

What is __ method __ in PHP?

"Method" is basically just the name for a function within a class (or class function). Therefore __METHOD__ consists of the class name and the function name called ( dog::name ), while __FUNCTION__ only gives you the name of the function without any reference to the class it might be in.

What is __ call () in PHP?

Method overloading ¶ __call() is triggered when invoking inaccessible methods in an object context. __callStatic() is triggered when invoking inaccessible methods in a static context. The $name argument is the name of the method being called.

What is magic methods in PHP with examples?

Magic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public.

What is Megic method?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.


2 Answers

If you do a var_dump on $name inside of __get, you'll see it contains name. The __get function is actually being called in __destruct. This is because $var1 is an accessible member, therefore, does not call the __get or __set functions.

From the PHP Documentation:

The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.

Since $var1 is defined and public, there is no call to the magic methods.

like image 122
Tim Cooper Avatar answered Oct 09 '22 11:10

Tim Cooper


You have a $var1 property in your class, so __set is not called : it's only called when there is no property with the name of the one you're trying to set.

Remove that public $var1 property, and __set will be called.


Still, even after removing that property, __get is still called.

If you put an echo at the end of your script, you'll see that __get is called after than echo -- i.e. after what seems to be the end of your script.


And if you take a look at your destructor method :

function __destruct() {
    print "<br/>Destroying " . $this->name . "\n";
}

You see that this destructor tries to read from a non-existant property -- hence the call to __get.

like image 35
Pascal MARTIN Avatar answered Oct 09 '22 11:10

Pascal MARTIN