Thursday, March 9, 2017

Return instance of class from base class

I'd like to create an object like so:

class Obj extends BaseModel {

    public function doSomething() {
        // do some stuff with the instance
    }
}



class BaseModel {

    public static function find($id){
        // retrieve object from database and return instance of Obj class
    }
}

So i can achieve the following:

$obj1 = Obj::find(1);
$obj1->doSomething();

How can I create this so that the static method from the base class returns an instance of the Obj class?

(similar to how Laravel handles objects)



via justijn

Advertisement