I'm new in PHP OOP. I need some help for how to write the php OOP class then can call the class like below.
Query->table('user')->column('id','name')->where('name LIKE ?',
["name"=> 'John'])->orderby('name', 'desc');
But, I had try so many time, what I can get it something like below then stop.
Query->table('user')->column('id','name');
I'm running out of ideas and I had google a lot, but fail to find any solution.
Any suitable help is very much appreciated.
You just need to return on each function of class object itself using $this
. All functions that you want to run "in chain" should be public
. Check this code:
<?php
class ClassName
{
public function a()
{
// ...
return $this;
}
public function b($param)
{
// ...
return $this;
}
public function c()
{
// ...
return $this;
}
}
// testing
$obj = new ClassName;
$result = $obj->a()->b('someParam')->c();
You should read about method chaining.