I have code that when heavily boiled down, looks pretty much like this:
abstract class Car {
public abstract static function parts();
protected static function getParts() {
$parts = [];
$properties = (new ReflectionClass(static::class))->getProperties();
foreach ($properties as $property) {
$parts[] = $property->getName();
}
return $parts;
}
}
class Ford extends car {
public $wheels;
public $body;
public static function parts() {
return self::getParts();
}
}
class FordTruck extends Ford {
public $truckBed;
}
function getBaseParts($cartype) {
return $cartype::parts();
}
But when I call getBaseParts("FordTruck")
I get back
array(3) {
[0]=>
string(8) "truckBed"
[1]=>
string(6) "wheels"
[2]=>
string(4) "body"
}
In reality, I only want back "wheels" and "body", not the 'extraneous' stuff that's added in classes past Ford
. I thought that creating a parts()
method in the class whose parts I care about, that then calls self::getParts()
, which in turn gets the properties from static
would mean that the class that 'static' is referring to would be the Ford
class. But it's more like it's calling parts()
from the FordTruck
class.
Is there a way I can get the functionality I'm looking for with this setup? The 'easiest' way I can think of would be moving the 'getParts()' method into the Ford
class and have it call a reflectionclass of self, but I have dozens of classes that extend Ford
that would all just be copied code if I fixed it that way.
source https://stackoverflow.com/questions/68154072/php-inheretence-of-methods-through-multiple-layers-of-classes
Comments
Post a Comment