今天我们向大家介绍的是关于可以联用->,如果一个对象的属性包含了一个对象,你可以使用两个->运算符来得到内部对象的属性。 你甚至可以用双重引用的字符串来放置这些表达式。 下面的例子中,对象House中的属性room包含了一组Room对象。
访问方法和访问属性类似。->运算符用来指向实例的方法。 在下面的中调用getLastLogin就是。方法执行起来和类外的函数几乎相同。
如果一个类从另一类中继承而来,父类中的属性和方法将在子类中都有效,即使在子类中没有声明。 像以前提到过的,继承是非常强大的。 如果你想访问一个继承的属性,你只需要像访问基类自己的属性那样引用即可,使用::运算符。
<ol class="dp-xml"> <li class="alt"><span><span>class Room </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public $name; </span></li> <li class=""><span> </span></li> <li class="alt"> <span> function __construct($</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"unnamed"</font></span><span>) </span> </li> <li class=""><span> { </span></li> <li class="alt"> <span>$this-></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; </span> </li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>class House </span></li> <li class=""><span>{ </span></li> <li class="alt"><span> //array(促销产品 主营产品) of rooms </span></li> <li class=""><span> public $room; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//create empty house </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">home</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> house; </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//add some rooms </span></li> <li class="alt"><span>$home->room[] = new Room("bedroom"); </span></li> <li class=""><span>$home->room[] = new Room("kitchen"); </span></li> <li class="alt"><span>$home->room[] = new Room("bathroom"); </span></li> <li class=""><span> </span></li> <li class="alt"><span>//show the first room of the house </span></li> <li class=""><span>print($home->room[0]->name); </span></li> <li class="alt"><span>?> </span></li> </ol>
PHP5.0对象模型有两个特殊的命名空间:parent命名空间指向父类,self命名空间指向当前的类。下面的例子中显示了如何用parent命名空间来调用父类中的构造函数。 同时也用self来在构造函数中调用另一个类方法。
<ol class="dp-xml"> <li class="alt"><span><span>class Animal //动物 </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public $blood; //热血or冷血属性 </span></li> <li class=""><span> public $name; </span></li> <li class="alt"> <span> public function __construct($blood, $</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>) </span> </li> <li class=""><span> { </span></li> <li class="alt"> <span>$this-></span><span class="attribute"><font color="#ff0000">blood</font></span><span> = $blood; </span> </li> <li class=""><span>if($name) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span> $this-></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>class Mammal extends Animal //哺乳动物 </span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public $furColor; //皮毛颜色 </span></li> <li class=""><span> public $legs; </span></li> <li class="alt"><span> </span></li> <li class=""> <span> function __construct($furColor, $legs, $</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>) </span> </li> <li class="alt"><span> { </span></li> <li class=""><span>parent::__construct("warm", $name); </span></li> <li class="alt"> <span>$this-></span><span class="attribute"><font color="#ff0000">furColor</font></span><span> = $furColor; </span> </li> <li class=""> <span>$this-></span><span class="attribute"><font color="#ff0000">legs</font></span><span> = $legs; </span> </li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>class Dog extends Mammal </span></li> <li class="alt"><span>{ </span></li> <li class=""><span> function __construct($furColor, $name) </span></li> <li class="alt"><span> { </span></li> <li class=""><span>parent::__construct($furColor, 4, $name); </span></li> <li class="alt"><span> </span></li> <li class=""><span>self::bark(); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> function bark() </span></li> <li class=""><span> { </span></li> <li class="alt"><span>print("$this->name says 'woof!'"); </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">d</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Dog("Black and Tan", "Angus"); </span> </li> <li class=""><span>?> </span></li> </ol>
对于对象的成员来是这样调用的:如果你需要在运行时确定变量的名称,你可以用$this->$Property这样的表达式。 如果你想调用方法,可以用$obj->$method()。
你也可以用->运算符来返回一个函数的值,这在PHP以前的版本中是不允许的。例如,你可以写一个像这样的PHP5.0对象模型的表达式: $obj->getObject()->callMethod()。这样避免了使用一个中间变量,也有助于实现某些设计模式,如Factory模式。