深入分析php之面向对象
author:一佰互联 2019-04-30   click:151

以前对面向对象仅限于死记硬背型,工作这么久了,回过头来看又是一翻体悟,供大家看看。
1.final
final:php5新增一个final关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法;如果一个类被声明final,则不能被继承。
复制代码 代码如下:
class BaseClass{
     public function test(){
          ehco "test";
     }     final public function moreTest(){
          echo "moretest";
     }
}class ChildClass extends BaseClass{
     public function moreTest(){
          echo "moretest";
     }
}
// 产生 Fatal error: Cannot override final method BaseClass::moretest()

2.__toString(建议用PHP5.2或者更高版本)
复制代码 代码如下:
class Person{
     protected $name;
     protected $email;     public function setName($name){
          $this->name = $name;
     }     public function setEmail($email){
          $this->email = $email;
     }     public function __toString(){
          return "$this->name <$this->email>";
     }
}
$rasums = new Person;
$rasums->setName("test");
$rasums->setEmail("test@qq.com");
print $rasums;

3.接口和抽象类
接口的作用:你想要保证一个类按照特定的名称、可见性和原型实现一个或多个方法。
接口的要求:
     类中全部为抽象方法
     抽象方法钱不用加abstract
     接口抽象方法属性为public
     成员属性必须为常量
例:
复制代码 代码如下:
interface ChildTest{
     public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
     public function childTest(){
          echo 1;
     }
     …………
}

抽象的作用: 其实抽象类和接口类有一部分很像,记得在哪里看见这样一句话,抽象类就把类像的部分抽出来,这句看上去很搞笑,其实它说出了抽象类的真理,抽象类的作用 是,当你发现你的很多类里面用很多方法你不断的在重复写,那你就可以考虑使用抽象类了,你可能会说“我不是可以重写一个类每个公共类我个实例化一个这个公 共类,调用相同的方法就可以了”,这里是可以,实际上抽象类做的工作也就是这个,不过他省去了你实例化的这个步骤,让你就像直接调用本类方法一样方便,而 且你还可以重载这个方法。
抽象的要求:
     类中至少有一个抽象方法
     抽象方法钱必须加abstract
例:
复制代码 代码如下:
abstract class Database{
     abstract public function connect();
     abstract public function query();
     abstract public function fetch();
     abstract public function close();
}

注:抽象方法不能定义为私有方法、不能定义为最终方法,因为它们需要被继承。4.传递对象引用
php4:所有“=”都是创建一个副本
php5:除了对象外,其他“=”进行赋值时,都是创建一个副本;而对象则是引用5.克隆对象
一、
聚合类:
__call方法简介:
当客户端代码用类中未定义的方法时,__call会被调用。
__call()接受两个参数,一个是方法名称,另一个是传递给要调用方法的所有参数(包括数组)
__call()方法返回的任何值都会返回给客户,将好像调用方式真实存在一样
例:
复制代码 代码如下:
class Address{
     protected $city;
     protected $country;

     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()."";
echo $test2->getName()."-".$test2->getCity()."";
//testname1-testcity2
//testname2-testcity2

6.重要属性访问(__set __get __isset __unset) __isset __unset5.1之后才有用
作用:拦截对属性的需求,为了提高分离的程度,还要实现__isset()和__unset(),以便当我们用isset来检测属性或者unset()来删除属性,来保证类的行为正确
例:
复制代码 代码如下:
class Person{
     protected $__data = array("email","test");

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

注意:
     这两个方法只会捕捉缺少的属性,如果你为你的类定义了一个属性,那么当访问这个属性时php不会调用__get()和__set();
     这两个方法完全破坏了任何属性继承的想法。如果父对象中有个 __get()方法,而你在子类中又实现了自己的__get()方法,那么你的对象不会正确的执行,因为父类的__get()方法永远不会被调用,当然可以用parent::__get()解决
缺点:
     速度相对较慢
     使用魔术访问器方法就不可能在使用反射类,如phpdocumentor这类的工具将代码自动文档化
     不能将其用于静态属性