June 16, 2013
Call function inside static function of the class
Kriss’s Question:
In laravel framework i often use – Model::find($id)->orderedBy(param);
I would like to know how to achieve such expression. I began with this:
Class Test
{
static public function write()
{
echo "text no 1";
function second()
{
echo "text no 2";
}
}
}
now i when i do
Test::write();
i get “text no 1”
what i want is to do:
Test::write()->second();
and get ” text no 2″
Unfortunately my way doesn’t work.
Is it possible ?
Excuse me for bad language – still learning.
Logically it is not possible, you cannot call second()
until you have called Test::write()
and you can call it later because afterward the PHP will redeclare the function. So you need to change your approach.
If you return an object from the write()
method it is possible.
Class Test
{
static public function write()
{
echo "text no 1";
return new Test(); //return the Test Object
}
function second()
{
echo "text no 2";
}
}
Now you can call Test::write()->second();