You are here: Re: Using a Resource as a Class Property « PHP Programming Language « IT news, forums, messages
Re: Using a Resource as a Class Property

Posted by Jerry Stuckle on 07/23/07 02:28

Michael Fesser wrote:
> .oO(Sanders Kaufman)
>
>> Michael Fesser wrote:
>>> Correct. PHP doesn't do that automatically, except if the child class
>>> doesn't have a constructor. Then PHP will call the one from the base
>>> class, if it exists. But as soon as your child class uses its own
>>> constructor, you have to manually call the parent one. Usually this
>>> should be the first action.
>> Got it - and I wish I'd got it a year ago!
>>
>> Is that a normal OOP way to do it - done that way in C++ and whatnot -
>> or is this unique to PHP?
>
> It works like that in all languages I know and it simply _has_ to work
> that way. The programmer has to be able to exactly define if and when a
> parent method will be called. Just consider this little example:
>
> class A {
> protected $data = NULL;
>
> public function __construct() {
> $this->doSomething();
> }
>
> private function doSomething() {
> // do something with $this->data
> }
> }
>
> class B extends A {
> public function __construct() {
> $this->data = 'foo';
> parent::__construct();
> }
> }
>
> Quite simple: A base class with a member variable and a method that
> performs some action with that. Since this is done in the constructor,
> every child class has to be able to initialize the data before calling
> the parent constructor, as you can see in B::__construct(). If PHP would
> automatically call the parent constructor, this would be impossible.
>
> Micha

Micha,

Generally unique to PHP. Java, C++ and SmallTalk all call the base
class constructor automatically before any code is executed in the child
class constructor.

Also, the parent should never have a dependency on the child class, and
the parent's class constructor should never depend on anything in the
child class other than what is passed to the constructor.

Your case is improper OO. What if an instance of A is created instead
of B? It wouldn't work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

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

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