PHP 5 Tutorial - __get() Magic Method
This tutorial will guide you through the __get() Magic Method. The __get Magic method in PHP5 gets called when accessing the value of an undeclared or undefined attributes of an class. With this method the programmer can keep track on the variables which are not defined inside the class.
Syntax:
function __get($data) { //$data holds the name of the undefined attributes getting called. }
Example: __get Magic Method Usage
< ? class magicmethod { function __get($data) { echo "Error accessing undefined attributes"; echo "attributes Called:".$data; } } $a = new magicmethod(); echo $a->setData; ?>
Output:
Error accessing undefined attributes
attributes Called:setData
Explanation for the Example:
Here i am trying to echo an attribute setData of magicmethod class.
But in the magicmethod class setData is not defined so the php compiler excutes __get() magic method and displays error message.
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Hello…Man i just love your blog, keep the cool posts comin..holy Tuesday