Reputation: 621
Sorry, for the badly phrased title. I don't know much object-oriented PHP, so, I couldn't come up with a better title (or an answer to my problem!).
Ok, so, I have something like this:
Class foo{
var $hello;
function foo(){
}
}
Class customfoo extends foo{
//No Constructor
}
Now, I wrote another class, let's call it customclass
and I want to use it in the customfoo
class. But, I don't just want to use it in the customfoo
class, I want it to be created as soon as the customfoo
class is created.
Ordinarily, I presume, you would just use the constructor for this, so something like:
Class customfoo extends foo{
var $custom;
function customfoo(){
$this->custom = new customclass();
}
}
However, customfoo
is a child class, so, I think the constructor would replace the constructor of the parent class, and I don't want that to happen. So, how can I make a class customclass
when customfoo
is first started? I guess, I could just write an arbitrary function and call it via some other function (that I am sure gets executed early on), but it would be nice to at least know how to do the above.
Upvotes: 0
Views: 59
Reputation: 4143
First, you should name your counstructors as __construct
.
As you have pointed out, the constructor you write will overwrite the parent's one, but you can always call it from the child class, like this:
Class customfoo extends foo{
var $custom;
function __construct(){
parent::__construct();
$this->custom = new customclass();
}
}
Upvotes: 3