PHP Magic Methods __set and __get

PHP Magic Methods __SET and __GET

With PHP OOP, you can manually set and get properties when you work with your object, or, you can use the __SET and __GET magic methods which are invoked automatically after you instantiate a class.

If you want guaranteed results and plan to set property names outside the class, making your own functions will allow you to manage public, protected and private properties in this manner.

On the other hand, if you want to print or use properties from a class that are protected or private, you cannot override them with __get and __set magic methods.

The following examples should make this a little more clear.


Manual Get and Set Methods

<?php

class Manual {
public $a = "A";
protected $b = array("a" => "A", "b" => "B", "c" => "C");
 
public function set_var($var){
$this->b = $var;
}

public function get_var(){
return $this->b;
}

}

$m = new Manual();
$var = array("x" => "X", "y" => "Y", "z" => "Z");
//$var = "help";
$m->set_var($var);
echo $m->get_var();
echo "<br/>";
print_r($m->get_var());

?>

Now, I will go over this. The variable was set and returned. Even though the variable $b was protected, the property can be overwritten. However, the same cannot be said for the set_var() and get_var() methods since changing any one of them them to protected or private would not work, thus no value would be returned from the get_var() method.

If you uncomment the line with $var = help, you can change the value from an array to a string. As you can see with this method, $protected $b was overwritten.

Now, let's move on and try this with __get and __set magic methods.

Magic __get and __set Methods

<?php

class magicMethod {
public $one = "A";
protected $two = array("a" => "A", "two" => "B", "c" => "C");

public $mycount = 0;

public function __get($v) {
$this->mycount = $this->mycount +1;
echo "$v";
return $this->two[$v]; 
}

public function __set($variable, $value){

}
}

$test = new magicMethod();
//$test->one = "TEST"; //can change
//$test->two = "foo"; //cannot change because not public scope inside class
echo $test->one.",".$test->two; 

?>

Here is how this example works. The __set magic method accepts 2 parameters. Meanwhile, the __get magic method accepts and passes on a single parameter. As you can see, the magic methods invoked and printed the $v as two. After that, it printed the public property value for $one followed by the returned value of $this->two[$v].

Now, you may have originally thought that the public value of $one would be followed by the followed by the code from the magic method which output two as as $v and $this->two[$v] as the second value of the protected $two array.

But, the magic methods ran first and output the value of $v which was two. After that, $test->one was printed since it was next in line. After that, the returned value was printed. Since $one was public, it bypassed the __set and __get methods.

If you uncomment the $test->one = "TEST" and $test->two lines, you will see that you can alter the public property but have no luck changing the protected property.