Pages

Sunday, September 15, 2013

OOPS-PHP-Magic Methods

1. __get($property)

Get value for a property set in a class foe example

 function __get($property)  
 {  
 return $this->properties[$property];  
 }  


Usage: echo $st->name;

2. __set($property,$value) Set value for a property
 function __set($property, $value)  
 {  
 $this->properties[$property]="AutoSet {$property} as: ".$value;  
 }  


Usage: $st->roll=16;

3. __call()
overloads any undefined function in a class.Thus we can also call an undefined function and show custom error message.

 function __call($method, $arguments)  
 {  
 echo "You called a method named {$method} with the following  
 arguments <br/>";  
 print_r($arguments);  
 echo "<br/>";  
 }  

Usage: $ol->notAnyMethod("boo");

No comments:

Post a Comment