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 Michael Fesser on 07/23/07 04:10

..oO(Jerry Stuckle)

>Michael Fesser wrote:
>
>> 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.
>
>Generally unique to PHP.

Not entirely.

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

It's been a long time since I worked with Java and C++, but it looks
like you're right in these cases. I came from the Pascal/Delphi world;
in Object Pascal you have to explicitly call inherited constructors and
destructors.

>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.

Correct, and I don't think this is an issue here. The parent will work
either way. OK, the same thing, slightly modified:

class A {
protected $data = NULL;

public function __construct() {
$this->initData();
$this->doSomething();
}

protected function initData() {
}

private function doSomething() {
// do something with $this->data
}
}

class B extends A {
public function __construct() { // could be removed now
parent::__construct();
}

protected function initData() {
parent::initData();
$this->data = 'foo';
}
}

The result is the same.

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

It would. A::doSomething() just has to check if $this->data is empty and
react accordingly.

While this example was rather abstract, I use something similar in my
own web framework, for example in my form handling components. The base
class of my form controls accepts an ID, a caption and a default value
in its constructor. One of the derived classes performs a special check
on the passed default value before calling the inherited constructor. Of
course I could write this in a different way, but in this special case
calling the parent constructor a bit later is a really handy feature.

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

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