Is it possible to instantiate a second class when the first gets instantiated without using a constructor?

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.

Answers


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();
    }
}

Need Your Help

How do I get rid of extra debug information inside IntelliJ?

intellij-idea rubymine

I am getting way too much debugging information inside IntelliJ and am not sure how to get rid of it??? This is for a ruby+rails application.

Java Swing: JWindow appears behind all other process windows, and will not disappear

java swing splash-screen jwindow

I am using JWindow to display my splash screen during the application start up. however it will not appear in front of all windows as it should, and it will not disappear as well.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.