I have the following setup:
abstract class AParentLy{
private $a;
private $b;
function foo(){
foreach(get_class_vars(get_called_class()) as $name => $value){
echo "$name holds $value";
}
}
}
class ChildClass extends AParentLy{
protected $c='c';
protected $d='d';
}
$object = new ChildClass();
$object->foo();
What I want it to output is:
c holds c
d holds d
What it does output is:
c holds c
d holds d
a holds
b holds
The get_called_class() method correctly outputs “ChildClass”
I’m fairly new to class inheritance in PHP and from what I can gather the problem lies somewhere in the so scope. But I can’t figure out how to make it work.
(The somewhat questionable solution would be to just go ahead and add a great big if($name!=’a’ && $name!=’b’ …~ into the mix. But I’m sure there must be another, more sane and stable way to do this)
Had another go at the whole experiment this question was a part of.
The eventual solution (just in case anyone stumbles over this and has the same problem) I came to was to create the following method within the parent class:
with this you get every parameter (and their value) of the child class without any of the parent class. You’ll have to change the function a bit if you ever change the class name, but at least for me this was a workable solution.