note 114160 added to language.oop5.overloading
| From: | NanheKumar | Date: | Fri, 17 Jan 2014 22:47:32 +0000 |
| Subject: | note 114160 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198271@lists.php.net to get a copy of this message | ||
<?php
//How can implement __call function you understand better
class Employee {
protected $_name;
protected $_email;
protected $_compony;
public function __call($name, $arguments) {
$action = substr($name, 0, 3);
switch ($action) {
case 'get':
$property = '_' . strtolower(substr($name, 3));
if(property_exists($this,$property)){
return $this->{$property};
}else{
$trace = debug_backtrace();
trigger_error('Undefined property ' . $name . ' in ' .
$trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
return null;
}
break;
case 'set':
$property = '_' . strtolower(substr($name, 3));
if(property_exists($this,$property)){
$this->{$property} = $arguments[0];
}else{
$trace = debug_backtrace();
trigger_error('Undefined property ' . $name . ' in ' .
$trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
return null;
}
break;
default :
return FALSE;
}
}
}
$s = new Employee();
$s->setName('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo $s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Notice: Undefined property setAge in
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/114160
Del: integrated -- https://master.php.net/note/delete/114160/integrated
Del: useless -- https://master.php.net/note/delete/114160/useless
Del: bad code -- https://master.php.net/note/delete/114160/bad+code
Del: spam -- https://master.php.net/note/delete/114160/spam
Del: non-english -- https://master.php.net/note/delete/114160/non-english
Del: in docs -- https://master.php.net/note/delete/114160/in+docs
Del: other reasons-- https://master.php.net/note/delete/114160
Reject -- https://master.php.net/note/reject/114160
Search -- https://master.php.net/manage/user-notes.php