Difference between php __set (), __get and simple setup, get function

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

Here is the code that sets the value in the 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 __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 can see, both of these functions do the same job.

I am confused by why PHP developers will use the __get / __set magic methods when they can be implemented by themselves?

I'm sure there is some benefit to them, but something is missing for me.

+4
source share
4 answers

This is not about what they do, but when called.

 __set() is run when writing data to inaccessible properties. __get() is utilized for reading data from inaccessible properties. 

So if you want

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

but this one

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

A very good example of using get and set methods here

Using MagicMethods simplifies reading and shortening code.

There are more interesting MagicMethods.

I also noticed that you are using the $store array, which is PUBLIC! This makes no sense. If you mark it as public, it will be accessible without using methods. It is not important. When you use methods or magic methods to get / set a variable, this is because the methods give you better control, when you want to assign or get a variable, you can do some checking, check for errors, etc. Creating variables as well-known, as a really bad convention, Access to open members can be obtained outside the class at every place in the project, if an error occurs, there is a lot of code to check when something goes wrong with the private member, accessible by the method, probably There are only two validation methods (get / set).

+12
source

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

These magic functions are actually made for some code design. Some people find that calling $object->property clearer than calling $object->getProperty() .

It is more clear when you use the chain, i.e. object->getDatabase()->getTable()->getRow()->getType()->convertToString()->makeUppercase() , etc. The use of "hidden properties" is more understandable.

Using magic methods does not solve some problems (for example, calling by reference), but sometimes they are easier to read.

Using properties is very popular in some languages ​​(e.g. Object Pascal, i.e. Delphi or FreePascal), where your class may have something like:

 property count: Integer read getCount write getCount; 

where both getCount() and setCount() become private or protected. An encoder may think of them as variables, but methods are called instead.

In PHP, there are also some advantages of the magic methods __get() (and __set() ). Usually, when you call a nonexistent property, a notification will be given. But if you implement the __get() method, no notification is given, and you can encode the __get() method, for example, to return NULL anyway if this property does not exist or handles errors or something else.

For what danger (but also opportunities) it may be, please take a look at the last example here .

+1
source

These magic methods are like shortcuts. Other languages, such as Python, C #, and Ruby, also implement these functions.

I think this is more useful when you need to put PHP code inside an HTML template:

 foreach($items as $item): ?> <li> Title: <?php echo $item->getTitle(); ?> <br/> Description: <?php echo $item->getLongDescription(); ?> <br/> URL: <?php echo $item->getAbsoluteUrl(); ?> <br/> </li> <?php endforeach; 

This is more readable the higher:

 foreach($items as $item): ?> <li> Title: <?php echo $item->title; ?> <br/> Description: <?php echo $item->longDescription; ?> <br/> URL: <?php echo $item->absoluteUrl; ?> <br/> </li> <?php endforeach; 

So, you are right, there is absolutely no difference, this is just a way to alleviate the situation.

0
source

__set() triggered when data is written to inaccessible properties.

__get() used to read data from inaccessible properties.

Ref.

 public void __set ( string $name , mixed $value ) 

EX.

 public mixed __get ( string $name ) 
0
source

Source: https://habr.com/ru/post/1481730/


All Articles