I have this code:
$db->get()->query()
Now i want the get
method's return to depend on:
$db->get()
return $db->query var;
but
$db->get()->query()
the get()
method will return $this
I have this code:
$db->get()->query()
Now i want the get
method's return to depend on:
$db->get()
return $db->query var;
but
$db->get()->query()
the get()
method will return $this
I am not sure what you are asking but
$db->get()->query()
is the same as :
$somevar = $db->get();
$somevar->query();
Updated: You can't change the return depending on whether you've chained the code (which is what it looks like you want to do) - however for this particular problem you can implement the too string method which will 'appear' to do the same thing :)
<?php
class DB {
private $query = 'some query';
public function get()
{
return $this;
}
public function query()
{
// do the query function
}
public function __toString()
{
return $this->query;
}
}
$db = new DB;
echo $db->get(); //prints 'some query'
$db->get()->query(); // runs the query() method
The function doesn't know the calling context, eg. $x = $db->get(); or $db->get()->query(); So it doesen't know what to return. Make another function or add a param, if you really have to use it like that.