Reply to Re: Using a Resource as a Class Property

Your name:

Reply:


Posted by Jerry Stuckle on 07/23/07 12:08

Michael Fesser wrote:
> .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.
>

Object Pascal is not a true OO language. It's an OO extension to a
structured language (and not a real good implementation at that - there
are several holes in the way it is implemented). The ones I mentioned
are true OO languages.

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

No, Micha, it is improper OO. The base class is depending on something
form the derived class. Additionally, in proper OO, A::data would be
private to the base class. And the constructor for A would be called
before the constructor to B ever got started.

That is a proper OO implementation, and follows OO theory and operations.

> 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

You should work on your OO techniques more. It ends up causing more
problems than it solves.


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

[Back to original 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

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