__set_state() Magic Method

Saira Sadiq
A free video tutorial from Saira Sadiq
Saira Sadiq
4.2 instructor rating • 1 course • 5,019 students

Learn more from the full course

OOP Object Oriented Programming In PHP 7

Learn advanced object oriented programming with simple code examples. This Course includes almost all OOP PHP7 topics.

05:45:48 of on-demand video • Updated April 2019

  • Most of the topics of object oriented programming given in PHP manual.
  • Apply Advanced OOP concepts in your applications.
  • Add OOP skill in your CV.
English __set_state() is the most trickiest method in all magic methods. So its ok if you watch this video again and again. OK. The static __set_state() magic method is called when the var_export() function is called on our object. So first a little detail about var_export() function. This method takes two parameters. First one is variable or object, second one is an optional parameter, These brackets show that this parameter is optional, It should be a boolean value which may be true or false. By default its value is false. var_export() returns a parsable string representation of a variable or an object, parsable string representation means it returns a string that represent a php code or in simple words this functions convert a variable or an object into a php code.... We can run that php code using PHP eval() function. Now if we give true as second parameter then var_export returns the php code as a string instead of outputting it and if false then it output the php code in the browser. OK. now here we have a class Test with two private properties, value 1 and value 2 and a constructor that is setting default values of these two properties. And here we are creating an object of test class and passing it to var_export() function. Now it should return a valid php code Let's see what it will print. Aaaand this is the php code that var_export() returns, This code is calling a set_state method of Test class and passing it an array of all properties and values Var_export() method is outputting the code in the browser. OK. Now I have made a little change here. As I told you If we pass it true as second parameter then it will return the php code as a string. So here we are passing true and then assigning the result to a variable strCode lets see the result. Nothing is printing because now result is in strCode variable and we have to print it using echo() It is exactly the same as we have already seen in the browser Now if we pass php code as a string to eval function it executes that code. So, lets pass this string Code variable to eval function to execute it. OK. And lets see the result again............ A fatal error that a call to undefined method of class Test, because we haven't defined set_state method in our Test class. It means var_export() cannot produce executable PHP code for the Test Class without a __set_state() method defined. So let's do it. but note one thing when set_state method is called an array of key value pair of properties is passed to it as an argument. So, we have to define set_state() method that is taking one array as a parameter OK now let's do it. Now here we have defined a static method __set_state(). that is accepting one array parameter. This array will be automatically given by php and it will be a key value pair of properties of current object of Test class. But what should we put in our __set_state() method? That's completely up to you. Whatever you do at the end It just should produce valid PHP code. And what we are doing in this method, here we are creating a new test object and setting the value of its both properties just changing the second property value a little bit and then returning this object and that’s it. Now when we will call var_export function on our test object, php will prepare a string that will be a php code and that code will be calling __set_state() method of the test class and an array of properties and their values will be passed to the method as an argument. And when this string will be passed to eval() function. It will execute it and set_state function will be called which will return a new test class object Now let's see this in the browser. No fatal error because we have defined set_state method in our class …..... that method is returning an object that is considered to be a valid php code So you can do anything in your __set_state() method but at the end it should return a valid PHP Code.