Req #14289 [Opn->Csd]: Member variables should be protected not private

From: Date: Mon, 28 Sep 2009 13:38:47 +0000
Subject: Req #14289 [Opn->Csd]: Member variables should be protected not private
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-1755@lists.php.net to get a copy of this message
Edit report at http://pear.php.net/bugs/bug.php?id=14289&edit=1 ID: 14289 Updated by: schmidt@php.net Reported By: pear at joestump dot net Summary: Member variables should be protected not private -Status: Open +Status: Closed Type: Feature/Change Request Package: Net_URL2 Operating System: All Package Version: 0.2.0 PHP Version: 5.2.0 -Assigned To: +Assigned To: schmidt Roadmap Versions: New Comment: -Status: Open +Status: Closed -Assigned To: +Assigned To: schmidt __get() and __set() were added in 0.3.0. Previous Comments: ------------------------------------------------------------------------ [2008-07-10 15:49:43] jstump A problem with testing is that I can't, without a doubt, know that $this- >_path actually contains what I think it should as getPath() may return something different than the actual variable. Let's use your example that it makes sense for $this->_path to be stored in an array. Let's say that getPath() returns a string. I can't really assert that the structure of that array is properly formed when it's private. If you'll add __get() and __set() and they'll be backwards compatible I'll shut up. :) ------------------------------------------------------------------------ [2008-07-10 15:42:15] schmidt > unless the class is meant to be a read-only singleton, there's really > no reason to make variables private I doubt there is general consensus about that in OO circles. I agree that encapsulation may make unit testing hard, but as long as all properties are paired with a setter, I don't think that is a concern in this case. My main concern is that by making the private fields protected or public, the inner workings of the class cannot be changed later on without breaking BC. For instance, it may turn out to be a good idea to represent the query string as an array rather than a string. WRT mocking and subclassing I don't understand why $this->_path = 'bar' is better than $this->setPath('bar') (except perhaps for being easier to read). Could you give an example of a problem that is caused by _path being private? I am not opposed to adding __get() and __set(), though. ------------------------------------------------------------------------ [2008-07-10 13:40:51] jstump Because there's no good reason to have these variables as private. It's not like Net_URL2 is meant to be read-only as you've added setters. At the very least it'd have been nice to use __set() and __get() instead since that would have been backwards compatible. I've seen the use of private variables discouraged in my own proposals and in others as well (Services_Facebook is an example). Since then I've been converted as, unless the class is meant to be a read-only singleton, there's really no reason to make variables private and only leads to problems mocking them up and extending them later on. ------------------------------------------------------------------------ [2008-07-05 16:50:38] schmidt All the private properties are accessible through get/set methods. Why not use these when testing or extending the class? ------------------------------------------------------------------------ [2008-07-03 15:39:33] jstump Description: ------------ With 0.2.0 things switched from public to the more aggressive private. I'm hoping to persuade you to use protected instead. Moving to protected allows for two things: 1.) Easily mockup Net_URL2 for testing 2.) Easier to work with when extending Net_URL2 Why extend Net_URL2 you ask? Glad you asked! Digg's Submission_URL extends from Net_URL2 and has all sorts of extra URL validation, checking and munging. I'm not suggesting you change to protected solely so I don't have to rewrite my own code, but I don't see why protected wouldn't be a happy medium :) Thanks! ------------------------------------------------------------------------ -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=14289&edit=1

« previous php.pear.bugs (#1755) next »