开发者

PHP OOP: Method Chaining

开发者 https://www.devze.com 2023-01-02 08:52 出处:网络
I have the following code, <?php class Templater { static $params = array(); public static function assign($name, $value)

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);

Ho开发者_运维百科w can I alter this script so I could use this?

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


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?


You shouldn't be using static members:

class Templater
{
    private array $params = [];

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

        return $this;
    }

    public function draw()
    {
        // do something with $this->params
    }
}

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


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);


////////

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);


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;
    }
}


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消