views:

214

answers:

5

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);
+2  A: 

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);
vartec
You need to add "return $this;" at the end of the assign() method otherwise the chaining won't work.
Rich Adams
right, PHP is not fluent by default
vartec
+5  A: 

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();
Artefacto
+5  A: 

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:

Gordon
A: 

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;
    }
}
Joseph Mastey
A: 
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();
Isis