以前对面向对象仅限于死记硬背型,工作这么久了,回过头来看又是一翻体悟,供大家看看。
1.final
final:php5新增一个final关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法;如果一个类被声明final,则不能被继承。
public function setcity($city){$this->city = $city;}
public function getcity(){return $this->city;}
public function setcountry($country){$this->country = $country;}
public function getcountry(){return $this->country;}
}
class person{
protected $name;
protected $address;
//浅克隆
public function __construct(){
$this->address = new address;
}
public function setname($name){
$this->name = $name;
}
public function getname(){
return $this->name;
}
public function __call($method,$arguments){
if(method_exists($this->address,$method)){
return call_user_func_array(array($this->address,$method),$arguments);
}
}
//深克隆
public function __clone(){
$this->address = clone $this->address;
}
}
$test1 = new person;
$test2 = clone $test1;
$test1->setname('testname1');
$test1->setcity('testcity1');
$test2->setname('testname2');
$test2->setcity('testcity2');
echo $test1->getname().'-'.$test1->getcity()."\n";
echo $test2->getname().'-'.$test2->getcity()."\n";
//testname1-testcity2
//testname2-testcity2
public function __get($property){
if(isset($this->__data[$property])){
return $this->__data[$property];
}else{
return false;
}
}
public function __set($property,$value){
if(isset($this->__data[$property])){
return $this->__data[$property] = $value;
}else{
return false;
}
}
public function __isset($property){
if(isset($this->__data[$property])){
return true;
}else{
return false;
}
}
public function __unset($property){
if(isset($this->__data[$property])){
return unset($this->__data[$property]);
}else{
return false;
}
}
}
$test = new person;
$test->email= 'test';
var_dump($test->email);
如对本文有疑问, 点击进行留言回复!!
[ACTF2020 新生赛]Include 1【文件包含】【读取代码】
算法题解 - 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者组
推荐一个程序员必备网站之一:Dev Hints ,非常实用!
网友评论