You are here: Re: Constructor as a "Reset" Button « PHP Programming Language « IT news, forums, messages
Re: Constructor as a "Reset" Button

Posted by Michael Fesser on 07/24/07 19:56

..oO(Sanders Kaufman)

>Andy Hassall wrote:
>
>> Hang on a minute - the constructor is called when creating a new object.
>> There's no old values, no old resources - you're creating a new object.
>
>That's ONE use for the constructor.

It's the only use. As it's name suggests - it's used to create an object
(and a destructor is called while cleaning up, respectively). In PHP the
constructor is more or less just for initialization, in other languages
it's also used to allocate memory for the new object.

>But as Jerry was telling me in the earlier thread (and which proved true
>in my implementation), when that class is the parent of another parent
>class, only the most childish constructor gets called.
>
>So, suppose I have clsGrandKid, which "extends" clsKid, which in turn is
>an extension of "clsMama".
>
>If clsGrandKid has a constructor, then clsKid's and clsMama's
>constructors don't get called - not until I manually call them from
>within clsGrandKid.

In PHP, yes. But this doesn't mean that you're supposed to call the
parent constructor multiple times or not at all! In PHP you might get
the result you expect, other compilers might kill you for doing that.

>> Perhaps you want a singleton class? This often fits well for databases
>> resources.
>
>Singleton's aren't really OOP.

They are perfectly OOP, whenever you have to make sure that there's
always exactly one (not more, not less) instance of a class. It's a
quite useful pattern for database objects or - as in my framwork - for
having a global application object. Firstly, it would be fatal if there
would be a second one, secondly, using the Singleton pattern allows
kinda "superglobal" access to the application object from everywhere.

Micha

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация