10分钟学会php面相对象基础(Ⅴ)

继续前面的

abstract 抽象方法和抽象类;只有方法名称,没有方法体{},用abstract修饰; 只要类中有一个抽象方法,这个类就必须定义为抽象类;

abstract function fun1();

abstract function fun2();
abstract class person(){
  abstract function born();
}
 作为一个驾校毕业的杂技演员,9.1开学,继续往下填;
 
interface 接口 为了满足类似多重继承的需求,php提供接口;
  1. 接口与其他类,抽象类 没有太多的不同,只有一些特殊的点需要注意:
  2. 接口中不会存在有方法体的方法 fun();
  3. 接口中不支持给变量,只支持const 这样给常量;
  4. 接口不给传参;
  5. 接口不能实例化;
  6. 接口可以被一个抽象类或者普通类或者另一个接口继承;
  7. 接口如果要被普通类继承,要用implements 而不是 extends;
  8. 前面提到过,要想实例化,一个类中就不能存在任何一个抽象的方法或属性;
interface ml{

  const pi='3.14';

  function tuo();

  function mo();

}

interface ml2 extends ml{

  function tian();

}



class ml3 implements ml2{

  function tuo(){}; function mo(){}; function tian(){};

}

 

//继承多个接口需要主意,先继承抽象类或类,在实现接口;

class ml4 extends ml3 implemtents m1,m2;

后面以实例详细说明,接口的使用

待补:

 

多态的应用

三大特效封装、继承之后,再补多态;

对于面向对象的程序来说,多态就是把子类对象赋值给父类引用,然后调用父类的方法,去执行子类覆盖父类的那个方法,但在PHP里是弱类型的,对象引用都是一样的不分父类引用,还是子类引用。

这里的例子就照抄了,因为举得例子非常好。再举个类似的例子,car,都有一个加油的方法,有的加93,有的95,有的97;

interface shape{
 function area();
 function circumference();
 const pi="3.14";
}

class rectangle implements shape{
    var $width;
    var $height;
    function __construct($width,$height){
         $this->height=$height;
         $this->width=$width;
    }
    function area(){
        $area = $this->height * $this->width;
        return $area;
    }
    function circumference(){
        $circumference = ($this->height + $this->width)*2;
        return $circumference;
    }
    
}
class circle implements shape{
    var $width;
    var $height;
    function __construct($r){
         $this->r=$r;
    }
    function area(){
        $area = shape::pi*(pow($this->r,2));
        return $area;
    }
    function circumference(){
        $circumference = 2*shape::pi*$this->r;
        return $circumference;
    }
    
}
$retangle1 = new rectangle('2', '2');
$circle1 = new circle('3');
echo $retangle1->area();
echo $retangle1->circumference();
echo $circle1->area();
echo $circle1->circumference();

 

serialize 和 unserialize串行化和解析

当需要将对象存储到数据库或进行网络传输时,需要将对象串行化,即转化为二进制编码。

用上面的代码继续写;

$retangle3 = new rectangle('2','2');

$otos = serialize($retangle3);

传输或存储

$retangle4 = unserialize($otos);

$retangle4->area();

 

__sleep() 和 __wakeup() 方法,用于 serialize前执行和在unserialize后马上执行的操作。

在PHP5中有两个魔术方法__sleep()方法和__wakeup()方法,在对象串行化的时候,会调用一个__sleep()方法来完成一 些睡前的事情;而在重新醒来,即由二进制串重新组成一个对象的时候,则会自动调用PHP的另一个函数__wakeup(),做一些对象醒来就要做的动作。

比如上面的例子中

class retangle implements shape{
    var $width;
    var $height;
    function __construct($r){
         $this->r=$r;
    }
    function area(){
        $area = shape::pi*(pow($this->r,2));
        return $area;
    }
    function circumference(){
        $circumference = 2*shape::pi*$this->r;
        return $circumference;
    }
    function __sleep(){  
    $arr = array($width);
return $arr; }
function __wakeup(){ $this->xx = '10'; } }

这样,当实例化对象

$re = new circle('2','3');

$string1 = serialize($re); //转为字符串了,这时候拿去存库里

$p2 = unserialize($string1); //我从数据库李拿出来,然后让他再变成对象;

这时候,因为sleep,$re的height属性值没了;

var_dump($retangle2->height);    返回 null;

 

 

 

 
 
posted @ 2016-08-31 18:00  拾阶而上的PM  阅读(243)  评论(0编辑  收藏  举报
本博内容由王子豪 www.07byte.com 提供