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

时间:2014-09-02 11:15:12

标签: php oop

我想要一个可以这样使用它的课程:

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

OR

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

OR

Thing::doImportantThing();

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

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

我必须在课堂上改变什么才能以我想要的方式使用它?我已在每个函数调用中返回Thing实例。

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

静态方法的一个好处是它们可以在对象上下文中工作,并且可以这样调用:$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();