Can you do this in PHP? I’ve heard conflicting opinions:
Something like:
Class bar {
function a_function () { echo "hi!"; }
}
Class foo {
public $bar;
function __construct() {
$this->bar = new bar();
}
}
$x = new foo();
$x->bar->a_function();
Will this echo “hi!” or not?
It’s perfectly fine, and I’m not sure why anyone would tell you that you shouldn’t be doing it and/or that it can’t be done.
Your example won’t work because you’re assigning
new Bar()to a variable and not a property, though.$this->bar = new Bar();