Home > PHP > PHP 5 Tutorial – __get() Magic Method

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;
 
?>

Your email:

 


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.


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: PHP Tags: ,
  1. November 6th, 2007 at 04:49 | #1

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

  1. No trackbacks yet.