2014-09-02 51 views
1

我想有一個類,我可以使用這種方式的靜態方法我只能這樣使用它:調用本身的對象

$thing = new Thing(); 
$thing->doThis()->doThat()->doImportantThing(); 

我在課堂上有什麼改變,所以我可以按我想要的方式使用它?我已經在每個函數調用中返回一個Thing實例。

我想用一個簡單的理由,想象一個郵件類,在構造函數中你定義了一個默認的from和to,但是你可能想要改變它,所以你做了Mail::setFrom()->send()。如果您想更改爲,則使用Mail::setTo()->send()。它只是讓它更容易使用,如果它將被不同的人用於不同的項目。

我想通過調用Mail::{something}有一個構造函數調用,然後運行{something}函數。

+2

我不禁納悶,爲什麼呢? – 2014-09-02 11:17:29

+0

也許[這](http://stackoverflow.com/questions/125268/chaining-static-methods-in-php)將幫助不知何故 – Sami 2014-09-02 11:32:31

+0

@Sami謝謝你,但這個問題部分幫助,但我想實現,通過這個例子,避免了'getInstance()'調用。 – 2014-09-02 11:35:51

回答

2

你可以做到這一點

class Thing { 

    public static function __callStatic($name, $arguments){ 
     $thing = new self; 
     return $thing->$name($arguments); 
    } 

    public function __call($name, $arguments){ 
     return $this->$name($arguments); 
    } 

    private function doThis(){ 
     echo 'this'.PHP_EOL; 
     return $this; 
    } 

    private function doThat(){ 
     echo 'that'.PHP_EOL; 
     return $this; 
    } 

    private function doImportantThing(){ 
     echo 'Important'.PHP_EOL; 
     return $this; 
    } 
} 

Thing::doThis()->doThat(); 
Thing::doThat()->doImportantThing(); 

這是一個非常醜陋的變通,雖然。它禁用你有私人方法。

DEMO

+0

我想用一個簡單的理由,設想一個郵件類,在構造函數中你定義了一個默認的'from'和'to',但是你可能想要改變它,所以你要做'Mail :: setFrom() - >發送()'。如果你想改變爲,你使用'Mail :: setTo() - > send()'。它只是讓它更容易使用,如果它將被不同的人用於不同的項目。 – 2014-09-02 11:30:44

+0

所以你想做方法鏈,或者只是靜態調用? – 2014-09-02 11:31:47

+0

@MichelTomé爲什麼不把可選參數傳遞給'send()'方法? – 2014-09-02 11:33:19

0

靜態方法的一個偉大的事情是,他們可以在目標環境中工作,可以這樣調用:$instance->staticMethod()

這是(即使你在IDE代碼完成,並可作爲axpected只要你想):

class Mail 
{ 
    public static $from; 
    public static $to; 
    public static $subject; 
    public static $message; 

    protected static $onlyInstance; 

    protected function __construct() 
    { 
     // disable creation of public instances 
    } 

    protected static function getself() 
    { 
     if (static::$onlyInstance === null) 
     { 
      static::$onlyInstance = new Mail; 
     } 

     return static::$onlyInstance; 
    } 

    /** 
    * set from 
    * @param string $var 
    * @return \Mail 
    */ 
    public static function from($var) 
    { 
     static::$from = $var; 
     return static::getself(); 
    } 

    /** 
    * set to 
    * @param string $var 
    * @return \Mail 
    */ 
    public static function to($var) 
    { 
     static::$to = $var; 
     return static::getself(); 
    } 

    /** 
    * set subject 
    * @param string $var 
    * @return \Mail 
    */ 
    public static function subject($var) 
    { 
     static::$subject = $var; 
     return static::getself(); 
    } 

    /** 
    * set message 
    * @param string $var 
    * @return \Mail 
    */ 
    public static function message($var) 
    { 
     static::$message = $var; 
     return static::getself(); 
    } 

    public static function send() 
    { 
     echo "<pre><b>Hurrah mail sent</b>" 
       . "\nFrom:\t ".static::$from."" 
       . "\nTo:\t ".static::$to." " 
       . "\nSubject: ".static::$subject."" 
       . "\nMessage: ".static::$message; 
     echo "</pre>"; 
    } 
} 

用法示例:

Mail::from('[email protected]') 
     ->to('[email protected]') 
     ->subject('hehe works') 
     ->message('your welcome') 
     ->send(); 

輸出

Hurrah mail sent 
From: [email protected] 
To:  [email protected] 
Subject: hehe works 
Message: your welcome 

例2(這也適用):

Mail::from('[email protected]') 
     ->to('[email protected]'); 

    Mail::subject('hehe works') 
     ->message('your welcome') 
     ->send();