(This went to me privately, but in order for this to benefit the discussion, I reply to the list, as well. Besides, there's now another thread for this, too)
Hi Richard. > When would the constructor be called automatically? I've used Delphi > and you use the inherit verb (or inherited - long time ago - can't > remember exactly). In PHP parent::__construct (though I think > parent::__METHOD__ would be cleaner as this removes ambiguity on the > actual name for all inherited methods). Well, let's look at C++, which I'm most familiar with. Here's an example: class Base { protected: Base(int a) { ... } }; class Derived : public Base { public: Derived() : Base(123) // Base class constructor called here { ... } } In C++, the order of construction goes from the top base class to the most derived class (although using virtual inheritance complicates that a little), and destruction happens in the reverse order. One important point is that the all the base class constructors (if any) are called before the derived class's constructor body (i.e. what's between "{" and "}") is entered. This way, you may rely on properly constructed base classes, as well as initialised member variables, in the constructor. The "Derived() : Base(123)" syntax is, as may be familiar, an "initialiser list", and may be used to initialise both any base classes, as well as member variables. With reference to another thread about uninitialised member variables: In C++, unless these are initialised using the initialiser list, they will be default-constructed, so they are also in a well-defined state, on entry to the constructor body. The same goes for any base classes. Now, what to do in PHP? As PHP is defined, the best might simply be to give an error/warning/notice, if a base class hasn't been initialised (i.e. had its constructor called) when the derived class constructor finishes. As PHP doesn't have concepts like initialiser lists, or default-construction of base classes and member variables (unless these are explicitly constructed in these lists), it may not be much point in trying to do it that way in PHP. Regards, Terje > On 14/09/06, Terje Slettebø <[EMAIL PROTECTED]> wrote: > > > Terje Slettebø wrote: > > > > The above was a contrived example, meant to illustrate the point. What's > > so > > > > bad about it? That it doesn't check the return value? > > > > > > I am not worried about the return value of the method. I am concerned > > > that $this->something is unset yet does not throw a notice. This is > > > only true for properties of objects. Any other variable in PHP does not > > > behave this way. IMHO, PHP should either initialize that variable to a > > > default type and value or throw a notice when you try and use it without > > > setting its value. > > > > I completely agree. :) _Then_ I understood you; I thought you meant my > > example(code) was "crap"... :) > > > > Another weird thing of PHP's implementation of OO is that propagation of > > constructor calls to the base class is not ensured, something I can't for > > the life of me understand why, but that deserves its own thread... > > > > Regards, > > > > Terje -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php