I have a class that is acting as a base class. I then have several other classes that inherit from it. I want to start loading the inherited classes using a static syntax, but the behavior isnt making much sense.
Until now i loaded the classes like this and it did the job.
$obj = new foo();
$something = $obj->ByID(1);
I want to be able to call the ByID function like this.
$something = foo::Get()->ByID(1);
The above code is working, but its not calling the inherited class, it is loading the base class. I think i can see why this is happening, but it doesnt make any sense. In .NET "this" will always apply to the inherited object, but it isnt working this way for me here.
I have a base class bar with the following method. This does not get overridden in the inherited class. In fact the inherited class only contains 1 single property.
class bar
{
public $directory;
public function Get()
{
return new self();
}
public function ByID($id)
{
//get the record from the file by using $directory to find the file.
}
}
class foo extends bar
{
public $directory = "/something/";
}
The problem im having is that when i load the class in a static way, self() is returning an object of the base class and not the inherited class. I need the $directory
property from the inherited class. The inherited classes will of course contain additional functions and other stuff, but right now this is a roadblock stopping me from implementing the code like this. Any idea what i am doing wrong?
I dont want to have to override Get() in every inherited class. With laravel i can remember the model class having a similar structure where you could create a model that had 1 property that was the name of the table and somehow it all worked fine.