PHP PHP5方法链接示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP5方法链接示例相关的知识,希望对你有一定的参考价值。

<?php
class DeadObnoxiousInfomercialSalesman {
   protected $name     = "Anonymous Salesman";
   protected $age      = "unknown";
   protected $products = array();
   public function name($name){
      $this->name = $name;
      return $this;
   }
   public function age($age){
      $this->age = $age;
      return $this;
   }
   public function sold($product){
      $this->products[] = $product;
      return $this;
   }
   public function __toString(){
      $out = sprintf("Dead salesman, %s, age(%s)", $this->name, $this->age);
      if($this->products){
         sort($this->products);
         $out .= sprintf(
            " sold the following products:\n%s",
            "  - ".join("\n  - ",$this->products)
         );
      }
      return $out;
   }
}

$person = new DeadObnoxiousInfomercialSalesman();

echo $person   ->name("BILLY MAYS")
               ->age(50)
               ->sold("Awesome Auger")
               ->sold("Big City Slider")
               ->sold("Dash Tray")
               ->sold("Hercules Hook")
               ->sold("Mighty Mendit");
?>

以上是关于PHP PHP5方法链接示例的主要内容,如果未能解决你的问题,请参考以下文章

PHP PHP5中的简单类示例

极光推送PHP5.1示例代码

为 PHP5.2- 框架提供 PHP5.3+ 命名空间的类名别名

php5.6 连接SQL SERVER

需要 PHP5 的示例 XML-RPC 客户端代码 [关闭]

PHP 5.4 的简化字符串偏移读取