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 17:09

..oO(Jerry Stuckle)

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

Depends on how you define "true OO language". Many people don't even see
Java as a true OO language (mainly because of the scalar types IIRC).
And when comparing C++ and Delphi, I clearly prefer the latter, its
object model has some really nice and handy features (properties with
read-only or write-only access for example, I like that).

After all it all comes down to how strictly you want to follow a
paradigm. It doesn't really matter if we're talking about OOP or MVC for
example. Some people prefer to follow the rules as strictly as possible,
but for me that would cause more troubles than it would solve. Sometimes
you simply have to think more practical than theoretical.

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

I still don't see it this way. The derived class just performs a
different initialization of the data. The base class will still work
with or without that, because it already has this method ifself. It's
just overwritten by the child classes as necessary. I could even have
declared A::doSomething() as abstract, so child classes would be forced
to overwrite/implement it.

It's not always possible to initialize an object just by passing all the
required data to the constructor, sometimes you have to perform
additional task in separate methods. That's not a problem. And calling a
child's method from a parent's method is not only allowed, but very
common and important (virtual methods, polymorphism, things like that).

> Additionally, in proper OO, A::data would be
>private to the base class.

Sure, can be done. But I prefer to declare my properties as protected,
if they are supposed to be accessed in derived classes. I don't want to
write getters and setters for every single member variable. While it
would be "true OO", it's usually too much work for nothing. Things are a
bit different when properties should be accessible from the outside, but
even then I sometimes simply declare them as public.

>And the constructor for A would be called
>before the constructor to B ever got started.

Could be done easily in the modified version of my example.

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

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