chams Posted December 6, 2011 Share Posted December 6, 2011 I stumbled upon this book where i first met __set(). When I research it, I met __get(). So there, I don't know who they are. And what they exactly do. If I may ask, is there any tutorial which can give me proper introduction with __set() and __get()? I have read for beginners, but I dont know, maybe I'm too dummy to understand. please bare with me i am just beginner. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/252552-help-with-understanding-the-magic-functions-__get-and-__set/ Share on other sites More sharing options...
btellez Posted December 6, 2011 Share Posted December 6, 2011 They are magic...They are used in object's to "automatically" add properties(class variables) on the fly. So if you don't need to define the variables in your class ahead of time if you define the magic method. The magic method is where you would define how your store your "dynamic variable"(for lack of better word). You would be able to access the property as if it were a public property of the class... Its more of an OOP topic....some of the OOP tutorials on PHPFreaks make mention of it but dont use it... Quote Link to comment https://forums.phpfreaks.com/topic/252552-help-with-understanding-the-magic-functions-__get-and-__set/#findComment-1294799 Share on other sites More sharing options...
scootstah Posted December 6, 2011 Share Posted December 6, 2011 __set() is used to assign class variables that don't exist. Likewise, __get() is used to return class variables that don't exist. You can see some examples on the php manual. http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members And to see all magic methods: http://www.php.net/manual/en/language.oop5.magic.php Quote Link to comment https://forums.phpfreaks.com/topic/252552-help-with-understanding-the-magic-functions-__get-and-__set/#findComment-1294804 Share on other sites More sharing options...
kicken Posted December 6, 2011 Share Posted December 6, 2011 Try having a read through this thread, as well as the manual link above for some more information / examples about the methods: Understanding get, set and property overloading Quote Link to comment https://forums.phpfreaks.com/topic/252552-help-with-understanding-the-magic-functions-__get-and-__set/#findComment-1294812 Share on other sites More sharing options...
ignace Posted December 6, 2011 Share Posted December 6, 2011 You can add properties dynamically to any object: class MyObject {} $obj = new MyObject(); $obj->propertyName = 'propertyValue'; print_r(get_object_vars($obj)); // Array ( [propertyName] => propertyValue ) This works even for classes that do not implement __set() and __get(). If you want to disable/overwrite this behavior then implement __set: class MyObject { public function __set($k, $v) {} } $obj = new MyObject(); // object $obj->propertyName = $propertyValue; print_r(get_object_vars($obj)); // Array ( ) This does not work if the property actually exists (that is: with a public access modifier) class MyObject { public $propertyName = ''; public function __set($k, $v) {} } $obj = new MyObject(); // object $obj->propertyName = 'propertyValue'; print_r(get_object_vars($obj)); // Array ( [propertyName] => propertyValue ) In Conclusion __set() and __get() allow you to create, read, and write to dynamic properties. A dynamic property is a property that has not been defined upon construction of the object (public $propertyName = '';. Quote Link to comment https://forums.phpfreaks.com/topic/252552-help-with-understanding-the-magic-functions-__get-and-__set/#findComment-1294842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.