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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With