Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between php __set(), __get and simple seting, getting function

I'm not sure what the value is in having __get and __set methods in PHP.

Here is the code which set the value in array.

class myclass {
public $sotre = array();

public function __set($arraykey,$value){
    echo 'Setting '.$arraykey.' to '.$value;
    $this->store[$arraykey] = $value;
} 
}

$obj = new myclass;
$obj->a = 'arfan';

Here is another code.

 class myclass {
public $sotre = array();

public function setvalue($arraykey,$value){
    echo 'Setting '.$arraykey.' to '.$value;
    $this->store[$arraykey] = $value;
} 
}

$obj = new myclass;
$obj->setvalue('a','arfan');

Both functions do the same thing.

Code using the __get/__set magic methods:

class myclass {
public $store = 
array(
    'a'=>'arfan',
    'b'=>'azeem',
    'c'=>'hader'
);

public function __get($arraykey){
    echo 'Getting array key '.$arraykey.'<br />';
    if(array_key_exists($arraykey,$this->store)){
        return $this->store[$arraykey];
    }
}

public function getvalue($arraykey){
    echo 'Getting array key '.$arraykey.'<br />';
    if(array_key_exists($arraykey,$this->store)){
        return $this->store[$arraykey];
    }
}
}

$obj = new myclass;
 echo $obj->a;

$obj = new myclass;
echo $obj->getvalue('a');

As you see these both function do same work.

I'm confused as to why PHP developers would use the magic methods __get/__set when they can be implemented yourself?

I'm sure there is some use for them but I must be missing something.

like image 508
Axeem Avatar asked Dec 11 '22 15:12

Axeem


2 Answers

It's not about what they do but when they are invoked.

__set() is run when writing data to inaccessible properties.

__get() is utilized for reading data from inaccessible properties.

So if you want to

echo $object->propertDoesnExists; //it will call get

but this

$object->propertDoesnExists = 1; //it will call set

The very good example of using get and set methods is here

Using MagicMethods makes code easier to read and shorter.

There are more interesting MagicMethods

I've also noticed that you use $store array which is PUBLIC! There is no sense in it. If you marks it as public it will be accessible without using methods. That is not a point of it. When you use methods or magic methods to get/set variable it is because methods gives you better control when you want to assign or get variable, you can do some validation, check against errors etc. Making variables as public is known as really bad convention. Public members can be accessed outside the class in every place of project, if error occurs there is a lot of code to check, when something goes wrong with private member accessed by method there is probably only 2 methods to check(get/set).

like image 115
Robert Avatar answered Jan 05 '23 21:01

Robert


Because it has been already told what the difference is, I will try to explain the purposes.

These magic functions are in fact made for some code clearance. Some people believe that calling $object->property is more clear than calling $object->getProperty().

It is more clear when you use chaining ie. object->getDatabase()->getTable()->getRow()->getType()->convertToString()->makeUppercase() etc. Using "hidden properties" is more clear.

Using magic methods does not solve some issues (like calling by reference) but sometimes it is easier to read.

Using properties is very popular in some languages (like Object Pascal, ie. Delphi or FreePascal), where your class can have something like:

property count: Integer read getCount write getCount;

where both getCount() and setCount() are made private or protected. The coder can think about them like they were variables, but methods are called instead.

In PHP there is also some advantage of the __get() (and __set()) magic methods. Normally, when you call non-existing property a notice would be given. But if you implement __get() method, no notice is given and you can code the __get() method for example for returning NULL in any case that property does not exists, or handling errors or whatever.

For what danger (but also possibilities) this can be, please take a look at the last example here.

like image 32
Voitcus Avatar answered Jan 05 '23 19:01

Voitcus