4

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
  • 1
    You may also want to modify the assign() method so it can accept an array of key => values. Commented Jun 7, 2010 at 15:52
  • You should be more precise in what you really want to do. Does a simple associative array served your purpose? Commented Jun 7, 2010 at 15:55

6 Answers 6

6

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:

Sign up to request clarification or add additional context in comments.

Comments

6

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

1 Comment

you can't chain off of a constructor like that!
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); 

1 Comment

You need to add "return $this;" at the end of the assign() method otherwise the chaining won't work.
1

////////

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

1 Comment

This answer is missing its educational explanation and proper formatting.
0

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.