Heim >Backend-Entwicklung >PHP-Tutorial >PHP 面向对对象基础(接口,类),php面向_PHP教程
介绍PHP面向对象的基础知识
1. 接口的定义interface ,类定义class,类支持abstract和final修饰符,abstract修饰为抽象类,抽象类
不支持直接实例化,final修饰的类/方法不能被继承/方法重写.
2. 接口的实现通过implements,类继承extends
<span>interface</span><span> IShape{ </span><span>function</span><span> draw_core(); }; </span><span>class</span> PathShape <span>implements</span><span> IShape{ </span><span>public</span> <span>function</span><span> draw_core(){} } </span><span>class</span> Rectangle <span>extends</span><span> PathShape{ </span><span>public</span> <span>function</span><span> draw_core(){ </span><span>//</span><span>overide draw_core</span> <span> } }</span>
3.静态变量和常量(static ,const )
a.常量声明变量名前面不需要加美元修饰符$,静态变量需要
b.两者都通过类访问,静态变量方法时候需要在变量名前加$美元修饰符好
<span>class</span><span> MyClass{ </span><span>const</span><span> M_CONST_VALUE; </span><span>static</span> <span>$M_STATIC_VALUE</span><span>; } MyClass</span>::<span>M_CONST_VALUE ; MyClass</span>::<span>$M_STATIC_VALUE</span>;
c.常量声明时候不支持访问权限修饰符,不能在const前加public,常量默认就是public。
<span>const</span> M_CONST ; <span>//</span><span>OK</span> <span>public</span> <span>const</span> M_CONST ; <span>//</span><span> throw exception</span>
4.类内部访问非静态/常量变量和方法通过$this,访问父类通过parent,在类内部访问静态变量和方法可以通过
self,self本质是指向该类也可以通过static访问
parent::method(); <span>//</span><span>父类方法</span> <span>$this</span>->method() ; <span>//</span><span>方法实例方法</span> self::<span>$static_value</span> ;<span>//</span><span>访问静态变量</span> <span>static</span>::<span>$static_value</span>;<span>//</span><span>同上</span>
5.static和self的区别在于self指的是解析上下文,也是是作用与当前类,static指的是被调用
的类而不是包含类,典型的例子就是单例
<span>abstract</span> <span>class</span><span> ParentClass{ </span><span>public</span> <span>static</span> <span>function</span><span> createInstance(){ </span><span>return</span> <span>new</span> <span>static</span><span>(); </span><span>//</span><span>这里不能使用self,因为self本意其实指向parentclass的 //如果你使用了self,那么将抛出异常,提示抽象类无法实例化 //而static并不直接指向parentclass而是作用与包含类 //</span> <span> } } </span><span>class</span> ChildClass <span>extends</span><span> ParentClass{ </span><span>// </span> }
7.类中使用拦截器,PHP拦截器有__get,__set,__inset,__unset,__call,这里只关注geth和set拦截器
__get(<span>$property</span><span>) 当访问未定义的属性时候该方法被调用 __set(</span><span>$property</span>,<span>$value</span><span>)当给未定义的属性赋值时被调用 </span><span>class</span><span> MyClass{ </span><span>public</span> <span>function</span> __get(<span>$property</span><span>){ </span><span>echo</span> "Access __get"<span>; </span><span>if</span>(property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>return</span> <span>$this</span>-><span>$property</span><span>; }</span><span>else</span><span>{ </span><span>return</span> "unknown"<span>; } } </span><span>public</span> <span>function</span> __set(<span>$property</span>,<span>$value</span><span>){ </span><span>if</span>(!property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>$this</span>->Name = <span>$value</span>; <span>//</span><span>变量不存在就直接给$Name赋值</span> <span> } } </span><span>public</span> <span>$Name</span> = "visonme"<span>; }; </span><span>//</span><span>访问</span> <span>$obj</span> = <span>new</span><span> MyClass(); </span><span>$obj</span>->Name ; <span>//</span><span>直接访问变量$Name</span> <span>$obj</span>->Password;<span>//</span><span>Password未定义,先访问__get最后输出unknown //-for __set</span> <span>$obj</span>->password = 'fz-visonme';<span>//</span><span>password不存在,那么将走__setz最后给$Name赋值</span> <span>echo</span> <span>$obj</span>->Name ; <span>//</span><span> output: fz-visonme</span>
8.类构造函数和析构函数:__construct, __destruct ,构造函数实例化对象时候调用,多用于成员变量初始化工作,析构在类销毁时候调用,多用于收尾工作
<span>class</span><span> MyClass{ </span><span>function</span><span> __construct(){} </span><span>function</span><span> __destruct(){} }</span>
9.对象的复制通过clone,clone关键字使用“值复制"方式来产生一个新的对象,对于对象复制本身还是走引用复制的。
a.简单类型赋值
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; }; </span><span>$a</span> = <span>new</span><span> MyClass; </span><span>$a</span>->ID = 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->ID; <span>//</span><span> output: 199</span>
b.包含对象的复制
<span>class</span><span> Account{ </span><span>public</span> <span>$RMB</span><span>; }; </span><span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } }; </span><span>$a</span> = <span>new</span> MyClass(<span>new</span><span> Account()); </span><span>$a</span>->AccountObj->RMB= 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 199</span> <span>$a</span>->AccountObj->RMB = 100<span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 100</span> <span> 在clone后,</span><span>$a的AccountObj改变时候</span>,同时会影响到<span>$b</span>
这种结果显然不是我们所期望的,我们所期望的是ab是两个不存在任何关联的独立对象.
为了解决这个问题我么可以在类内部实现__clone,当我们在外面调用clone时候其内部会调用类的__clonef方法,所以我们可以通过重写__clone来达到对clone的控制.例如针对b例子的改造
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } </span><span>//</span><span>__clone实现clone的控制 //这里内部同时对Account实现一次clone,这里就可以避免b例子中出现的问题</span> <span>public</span> <span>function</span><span> __clone(){ </span><span>$this</span>->ID = 0 ; <span>//</span><span>将ID置为0,如果你需要的话</span> <span>$this</span>->AccountObj = <span>clone</span> <span>$this</span>-><span>AccountObj; } };</span>
关于__clone方法我们需要知道,该方法是在被clone后的对象上调用,而不是在原始的对象上面运行的,例如上b例子中
$b = clone $a ; //执行的过程: 基本复制对象$a ---> $b执行__clone()