PHP 5 引入了新的对象模型(Object Model)。完全重写了 PHP 处理对象的方式,允许更佳性能和更多特性。
一、基本概念
1、class
每个类的定义都以关键字 class 开头,后面跟着类名,可以是任何非 PHP 保留字的名字。后面跟着一对花括号,里面包含有类成员和方法的定义。伪变量 $this 可以在当一个方法在对象内部调用时使用。$this 是一个到调用对象(通常是方法所属于的对象,但也可以是另一个对象,如果该方法是从第二个对象内静态调用的话)的引用。看下面例子:
Example#1 面向对象语言中的 $this 变量
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">class</span><span> A </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="keyword">function</span><span> foo() </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="keyword">if</span><span> (isset(</span><span class="vars">$this</span><span>)) { </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'$this is defined ('</span><span>; </span> </li> <li> <span class="func">echo</span><span> get_class(</span><span class="vars">$this</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">")n"</span><span>; </span> </li> <li> <span>} </span><span class="keyword">else</span><span> { </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"$this is not defined.n"</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"> <span class="keyword">class</span><span> B </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">function</span><span> bar() </span> </li> <li><span>{ </span></li> <li class="alt"><span>A::foo(); </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="vars">$a</span><span> = </span><span class="keyword">new</span><span> A(); </span> </li> <li class="alt"> <span class="vars">$a</span><span>->foo(); </span> </li> <li><span>A::foo(); </span></li> <li class="alt"> <span class="vars">$b</span><span> = </span><span class="keyword">new</span><span> B(); </span> </li> <li> <span class="vars">$b</span><span>->bar(); </span> </li> <li class="alt"><span>B::bar(); </span></li> <li><span>?> </span></li> </ol>
上例将输出:
<ol class="dp-xml"> <li class="alt"><span><span>$this is defined (a) </span></span></li> <li><span>$this is not defined. </span></li> <li class="alt"><span>$this is defined (b) </span></li> <li><span>$this is not defined. </span></li> </ol>
Example#2 简单的类定义
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">class</span><span> SimpleClass </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="comment">// 成员声明 </span><span> </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var</span><span> = </span><span class="string">'a default value'</span><span>; </span> </li> <li> <span class="comment">// 方法声明 </span><span> </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> displayVar() { </span> </li> <li> <span class="func">echo</span><span> </span><span class="vars">$this</span><span>-></span><span class="keyword">var</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>
Example#3 类成员的默认值
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">class</span><span> SimpleClass </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="comment">// 无效的类成员定义: </span><span> </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var1</span><span> = </span><span class="string">'hello '</span><span>.</span><span class="string">'world'</span><span>; </span> </li> <li> <span class="keyword">public</span><span> </span><span class="vars">$var2</span><span> = </span> </li> <li class="alt"><span>hello world </span></li> <li><span>EOD; </span></li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var3</span><span> = 1+2; </span> </li> <li> <span class="keyword">public</span><span> </span><span class="vars">$var4</span><span> = self::myStaticMethod(); </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var5</span><span> = </span><span class="vars">$myVar</span><span>; </span> </li> <li> <span class="comment">// 正确的类成员定义: </span><span> </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var6</span><span> = myConstant; </span> </li> <li> <span class="keyword">public</span><span> </span><span class="vars">$var7</span><span> = self::classConstant; </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$var8</span><span> = </span><span class="keyword">array</span><span>(true, false); </span> </li> <li><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>
2、new
要创建一个对象的实例,必须创建一个新对象并将其赋给一个变量。当创建新对象时该对象总是被赋值,除非该对象定义了构造函数并且在出错时抛出了一个异常。
Example#4 创建一个实例
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="vars">$instance</span><span> = </span><span class="keyword">new</span><span> SimpleClass(); </span> </li> <li class="alt"><span>?> </span></li> </ol>
复制代码当把一个对象已经创建的实例赋给一个新变量时,新变量会访问同一个实例,就和用该对象赋值一样。此行为和给函数传递入实例时一样。可以用克隆给一个已创建的对象建立一个新实例。
Example#5 对象赋值
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="vars">$assigned</span><span> = </span><span class="vars">$instance</span><span>; </span> </li> <li class="alt"> <span class="vars">$reference</span><span> =& </span><span class="vars">$instance</span><span>; </span> </li> <li> <span class="vars">$instance</span><span>-></span><span class="keyword">var</span><span> = </span><span class="string">'$assigned will have this value'</span><span>; </span> </li> <li class="alt"> <span class="vars">$instance</span><span> = null; </span><span class="comment">// $instance and $reference become null </span><span> </span> </li> <li> <span>var_dump(</span><span class="vars">$instance</span><span>); </span> </li> <li class="alt"> <span>var_dump(</span><span class="vars">$reference</span><span>); </span> </li> <li> <span>var_dump(</span><span class="vars">$assigned</span><span>); </span> </li> <li class="alt"><span>?> </span></li> </ol>
复制代码上例将输出:
<ol class="dp-xml"> <li class="alt"><span><span>NULL </span></span></li> <li><span>NULL </span></li> <li class="alt"><span>object(SimpleClass)#1 (1) { </span></li> <li> <span>["var"]=</span><span class="tag">></span><span> </span> </li> <li class="alt"><span>string(30) "$assigned will have this value" </span></li> <li><span>} </span></li> </ol>
3、extends
一个类可以在声明中用 extends 关键字继承另一个类的方法和成员。不能扩展多个类,只能继承一个基类。
被继承的方法和成员可以通过用同样的名字重新声明被覆盖,除非父类定义方法时使用了 final 关键字。可以通过 parent:: 来访问被覆盖的方法或成员。
Example#6 简单的类继承
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">class</span><span> ExtendClass </span><span class="keyword">extends</span><span> SimpleClass </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="comment">// Redefine the parent method </span><span> </span> </li> <li class="alt"> <span class="keyword">function</span><span> displayVar() </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"Extending classn"</span><span>; </span> </li> <li><span>parent::displayVar(); </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"> <span class="vars">$extended</span><span> = </span><span class="keyword">new</span><span> ExtendClass(); </span> </li> <li> <span class="vars">$extended</span><span>->displayVar(); </span> </li> <li class="alt"><span>?> </span></li> </ol>
上例将输出:
<ol class="dp-xml"> <li class="alt"><span><span>Extending class </span></span></li> <li><span>a default value </span></li> </ol>
1