note 114196 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:31:07 +0000 |
| Subject: | note 114196 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211907@lists.php.net to get a copy of this message | ||
Note Submitter: Nanhe Kumar
----
<?php
//How can implement __call function
class Student {
protected $_name;
protected $_email;
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{
echo "Undefined Property";
}
break;
case 'set':
$property = '_' . strtolower(substr($name, 3));
if(property_exists($this,$property)){
$this->{$property} = $arguments[0];
}else{
echo "Undefined Property";
}
break;
default :
return FALSE;
}
}
}
$s = new Student();
$s->setNam('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo $s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Undefined Property
?>