在对象上调用自身的静态方法


Static method that calls itself on an object

我想要一个可以这样使用的类:

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

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

Thing::doImportantThing();

但目前我只能这样使用它:

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

我必须在课堂上改变什么,这样我才能随心所欲地使用它?我已经在每个函数调用中返回了一个Thing实例。

我想使用它的原因很简单,想象一个邮件类,在构造函数中定义一个默认的from和to,但你可能想更改它,所以你做Mail::setFrom()->send()。如果要将更改为,请使用Mail::setTo()->send()。如果不同的人要在不同的项目中使用它,它只会让它更容易使用。

我希望通过调用Mail::{something}来进行类似构造函数的调用,然后运行{something}函数。

您可以执行此

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

静态方法的一个优点是它们可以在对象上下文中工作,并且可以这样调用:$instance->staticMethod()

它就在这里(即使你在ide中完成代码,也可以随心所欲地工作):

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('george@garcha')
        ->to('michel@tome')
        ->subject('hehe works')
        ->message('your welcome')
        ->send();

输出

Hurrah mail sent
From:    george@garcha
To:      michel@tome 
Subject: hehe works
Message: your welcome

示例2(这也适用):

    Mail::from('george@garcha')
        ->to('michel@tome');
    Mail::subject('hehe works')
        ->message('your welcome')
        ->send();