2016-06-15 84 views

回答

1

你只需要返回對象所有的時間。它被稱爲fluent interface。它可以是自己或其他物體。

<php 
class A 
{ 
    public function first() 
    { 
     // Do something 

     return $this; 
    } 
    public function callSecond() 
    { 
     // Do somewthing else 

     return $this; 
    } 
} 

$a = (new A())->first()->callSecond(); 
+0

你現在爲這個? http://stackoverflow.com/questions/37823434/php-how-can-close-the-html-tags-from-this-array –

1
<?php 
    class OtherClass{ 
     public function callSecond(){ 
      echo 'Second Called'; 
     } 
    } 

    class MyClass{ 
     public function first(){ 
      return new OtherClass(); 
     } 
    } 

    $myClass = new MyClass(); 
    $myClass->first()->callSecond(); 
?> 
+0

非常感謝我的朋友:) –

+0

沒問題,先生,沒問題 – ksealey

+0

現在一些對象爲了這? http://stackoverflow.com/questions/37823434/php-how-can-close-the-html-tags-from-this-array –