Re: how to mark protected properties?

From: Date: Tue, 08 Jun 2004 08:22:30 +0000
Subject: Re: how to mark protected properties?
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-30135@lists.php.net to get a copy of this message
Mika Tuupola wrote: >On Mon, 7 Jun 2004, Hans Lellelid wrote: > >> > 100% pure encapsulation is not efficient >> >> I would say encapsulation is at least half the point of using OO in the >> first place, so I'd heartily agree with Bertrand. Everything should be >> private/protected and hence prefixing every var with '_' seems to make >> little sense. > > Must say I agree here. Efficient is more than just > finely tuned fast running code. Efficient is also > easily maintenable code. Accessing class properties > directly degrades maintainability. > > Besides, I doubt providing accessor methods would sacrifice > speed too much unless you code is already perfect in > other areas. I would say to those worried about the loss of performance introduced by accessors that you can always buy new servers with more CPU and memory. In the end, you will be happy that you designed your classes wisely and didn't allow users to mess with your instance variables. I am pretty sure that in 2 years of time, a lot of good PHP classes will default instance vars to protected, just to be safe from users messing with them. Adding an accessor to your code is a design decision and has to be thought about beforehand. The accessor might take care of more things than just setting the instance variable value. If you don't make your variables protected, then you allow the users to play with what they want in your code, it is like you allow them to rape your classes. I say no way ! :) I fix rules and they have to play with my rules to use my code. If someone does not play with these rules, then he might be stuck later on because the code might change and he didn't foresee it. If you get rid of protected as suggested by Alan, you might as well realize on time that something private needs to become public or the opposite. This happened to me before. In PHP4, you end up with $obj->_privateVar = 'value'; which is a total nonsense. In PHP5, you end up with "Err, excuse me, I had to change the API a bit. Sorry about that guys :)". Use protected and accessors where needed and you are safe. If you want my opinion, we'd rather forget about public and use private with care. For those worried about the number of accessors they might have to write when using protected, there are ways to make it lighter with PHP. In the end, this is a philosophical debate and it really depends on which level of flexibility you want to give to your users and how safe you want to be. It is good to have the choice and it is good that PHP5 offers a better object model to those who want to use it. Bertrand Mansion Mamasam

« previous php.pear.dev (#30135) next »