For those who are familiar with Zend framework, what does each method return? I mean, I don’t understand how you can do method chaining and also have a value stored in each method? And does getRequest() pass the information on to getPost() method? How does that all work?
I tried doing this:
class test {
public function getRequest() {
array('Request' => 1, 'Request' => 2);
return $this;
}
public function getPost() {
$var = 'POST';
return $this;
}
}
$test = new test();
print_r($test->getRequest()->getPost());
And of course it returned an empty object. I’m not sure on how to fully explain it… unfortunately…
<?php
class ChainTest
{
public $val = null;
public function methodOne ( $val ) {
$this->val = $val;
return $this;
}
public function methodTwo ( $add ) {
$this->val = $this->val . $add;
return $this;
}
}
$t = new ChainTest;
$t->methodOne( 'Cookies' )->methodTwo( ' Nomnomnom!' );
var_dump( $t->val );
Method one returns an object (itself but it can be any object) you are then allowed to send commands on that object returned. You can issue commands to that object without assigning it to a variable or using $t-> if it the same object. Method One does not send Method Two anything using “return $this”. Object properties handle that.
class Posts {
protected $request = null;
public function getRequest() {
$this->request = $_SERVER['REQUEST_METHOD'];
return $this;
}
public function getPost() {
return $this->request;
}
}
$posts = new Posts();
var_dump($posts->getRequest()->getPost());
That seems to clear it up for me. Now had I wanted to continue chaining I would’ve needed to return itself once again in the getPost() method and eventually return the value of the property I’m looking for correct?