PHP OOP: Method Chaining

2019-01-28 07:00发布

问题:

I have the following code,

<?php
class Templater
{
    static $params = array();

    public static function assign($name, $value)
    {
        self::$params[] = array($name => $value);
    }

    public static function draw()
    {
        self::$params;
    }
}


 $test = Templater::assign('key', 'value');
 $test = Templater::draw();
 print_r($test);

How can I alter this script so I could use this?

$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);

回答1:

You cannot use Method Chaining with static methods because you cannot return a class level scope (return self won't do). Change your methods to regular methods and return $this in each method you want to allow chaining from.

Notice that you should not use T_PAAMAYIM_NEKUDOTAYIM to access instance methods as it will raise an E_STRICT Notice. Use T_OBJECT_OPERATOR for calling instance methods.

Also see:

  • Chaining Static Methods in PHP?


回答2:

You shouldn't be using static members:

class Templater
{
    private $params = array();

    public function assign($name, $value)
    {
        $this->$params[$name] = $value;
        return $this;
    }

    public function draw()
    {
        //not really sure what you want here
    }
}

$test = new Templater()->assign('key', 'value')->assign('key2', 'value2')->draw();


回答3:

Just use instance variables and instance functions instead of static ones.

<?php
class Templater
{
    $params = array();

    public function assign($name, $value)
    {
        $this->params[] = array($name => $value); 
        return $this;
    }

    public function draw()
    {
        echo $this->params;
        return $this;
    }
}

$test = new Templater();
$test->assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);


回答4:

////////

class Templater { static $params = array();

public static function assign($name, $value)
{
    self::$params[] = array($name => $value);
    return new Templater;
}

public static function draw()
{
    return self::$params;
}

}

$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw(); print_r($test);



回答5:

Mixing a static and an instance call like that is poor form in general, omitting that one (unless you give a reason that it needs to be static). The other concept you're working with is call chaining, which is implemented using returns.

class Templater
{
    protected $params = array();

    public function assign($name, $value) {
        $this->params[] = array($name => $value);
        return $this;
    }

    public function draw() {
        // do drawing w/ $this->params;
        return $this;
    }
}


回答6:

class Templater
{   
    public static $params;

    private static $_instance = null;

    public static function init()
    {
        if (self::$_instance === null)
        {
            self::$_instance = new self;
        }

        return self::$_instance;
    }

    public function assign($name, $value)
    {
        self::$params[$name] = $value;
        return $this;
    }

    public function draw()
    {
        return self::$params;
    }
}

$test = Templater::init()->assign('key', 'value')->assign('key2', 'value2')->draw();