Hey, everyone. I couldn't find anything Googling this problem, and I've found really good answers to some questions on SO before, so I'm taking this excuse to join the community.
I'm creating a hierarchy of classes for a PHP project I'm working on, and I'd like to have some variables in the classes initialized within the constructor function without explicitly writing the initialization code. Specifically I want to have the interpreter assume that some of the variables are actually pointers to a certain class. If I could do something like C structs, that would be pretty close to what I want.
So far, the only thing I came up with is to explicitly state the variable type within its own name and have the class call an initializing function on each, like;
class A{
...
}
class B{
var $x_A;
function initVar($var){
list($varname, $vartype) = split('_',$var);
$this->$var = new $vartype();
}
}
And B's constructor calls initVar on all of its get_class_vars(get_class($this)), so anything that inherits from it will do initialization in the same way; obviously including a check on the variable name, a check that the class exists, and a better separation scheme than a single underscore. I just cannot help but think that there is a better way to do this that isn't hardcoding the initialization into the construction function.
If anyone knows of a better way to do this, your help would be much appreciated.