Re: OOP::problem?

From: Date: Thu, 03 Apr 2003 17:43:29 +0000
Subject: Re: OOP::problem?
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-4631@lists.php.net to get a copy of this message
On Tue, 01 Apr 2003 10:47:20 +0200, matthias.nothhaft@gmx.de (Matthias Nothhaft) wrote: >Hi all, > >this is my first mail to this list, >but I don't know if this is the right one >for my problem, because it is not really depending >on pear, but I hope there are some php devs, who >can help me? > >Ok, I have a little idea to use it in PEAR... >I promise I'll tell it in my next posting ;-) > > >Take a look at my example: > ><?php > >class a >{ > function test($s) > { > $this->myvar = $s; > } >} > >class b >{ > var $myvar; > > function b($s) > { > a::test($s); > > echo $this->myvar; > } >} > >$b =& new b("hello world!"); > >?> > >The output is... "hello world!" > >Now my "problem": Is this a bug or a feature, >that I can access to class variables of any object >by using $this->anyvar in a class::function() ? > > >Thanks, > >Matthias > Hello Matthias, I'm not sure that it is either. It is mentioned a few times in the online documentation. Being able to access member variables of an object directly is not true OOP. However, OOP is only a concept and many different programming languages implement it differently. Right now, if you develope classes with PHP, you should provide accessor methods for the properties that need to be accessed from outside. This does not prevent the users of your class from accessing it's properties directly. However, it allows you to rearange, rename, etc anything you like inside your class in later releases as long as the same interface is provided. This way, if the users of your class do decide to access properties directly, they do so at their own risk. I have not checked myself yet, but I have heard rumors that PHP5 is going to be much more object oriented which could be a good thing. We'll see. Dan

« previous php.pear.general (#4631) next »