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.
Model::find($id)->orderedBy(param)
just means that static method find
returns object, who's method orderBy
is then executed.
Example:
Class Test1
{
public function say_hello()
{
echo 'Hello!';
}
}
Class Test2
{
static public function write()
{
$obj = new Test1();
return $obj;
}
}
Test2::write()->say_hello();
Class Test
{
static public function write()
{
echo "text no 1";
return new Test();
}
function second()
{
echo "text no 2";
}
}
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();