SchweppesAle Posted October 6, 2009 Share Posted October 6, 2009 Hi, I was wondering if the following code provides adequate encapsulation. The fact that __construct is not set to private makes me question whether I'm even doing this correctly. <?php class config{ private $_username; private $_password; private $_listid; function __construct(){ $this->_username = 'somelogin'; $this->_password = 'thepassword'; $this->_listid = '202'; } function get_username() { return $this->_username; } function get_password() { return $this->_password; } function get_listid() { return $this->_listid; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/ Share on other sites More sharing options...
KevinM1 Posted October 6, 2009 Share Posted October 6, 2009 If you're hard wiring your data within a class (not a good idea in most cases), then you don't need an explicitly defined constructor at all. Simply assign the data to the data members directly: class config { private $_username = 'somelogin'; private $_password = 'thepassword'; private $_listid = '202'; //etc. } Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/#findComment-931524 Share on other sites More sharing options...
Mark Baker Posted October 6, 2009 Share Posted October 6, 2009 __construct() shouldn't ever be set to private, otherwise you could never instantiate a class because that can only ever be done from outside the class ($x = new myClass()) which calls the constructor... if the constructor was private, then it can't be called from outside. However, your getters should be public, otherwise you have no means of accessing the attributes of the class Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/#findComment-931528 Share on other sites More sharing options...
SchweppesAle Posted October 6, 2009 Author Share Posted October 6, 2009 If you're hard wiring your data within a class (not a good idea in most cases), then you don't need an explicitly defined constructor at all. Simply assign the data to the data members directly: class config { private $_username = 'somelogin'; private $_password = 'thepassword'; private $_listid = '202'; //etc. } You're right, what do you recommend though? Should I just place the data outside the class then use the "require_once" function when it's needed? Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/#findComment-931542 Share on other sites More sharing options...
KevinM1 Posted October 6, 2009 Share Posted October 6, 2009 __construct() shouldn't ever be set to private, otherwise you could never instantiate a class because that can only ever be done from outside the class ($x = new myClass()) which calls the constructor... if the constructor was private, then it can't be called from outside. However, your getters should be public, otherwise you have no means of accessing the attributes of the class Correct. Typically constructors are marked as private (and, in some cases, both private and final) when objects aren't supposed to be directly instantiated. The Singleton (anti?) pattern relies on this. Factories are also a common place to see this, as they're most often interacted with via static methods that return the correct child object, and are often Singletons themselves. Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/#findComment-931547 Share on other sites More sharing options...
KevinM1 Posted October 6, 2009 Share Posted October 6, 2009 If you're hard wiring your data within a class (not a good idea in most cases), then you don't need an explicitly defined constructor at all. Simply assign the data to the data members directly: class config { private $_username = 'somelogin'; private $_password = 'thepassword'; private $_listid = '202'; //etc. } You're right, what do you recommend though? Should I just place the data outside the class then use the "require_once" function when it's needed? Well, it depends on the data and how you're going to use it. For a lot of basic config data, storing it in a separate location (like, say an XML file, or a file you can directly require_once()) and reading it into the system is the way to go. This promotes flexibility and modularity as you don't have to change your class code when migrating environments, only the environmental data. Just be sure to store it in a secure place (outside the web root) and thoroughly scrub the data before attempting to access the db or any other critical component of your system. Quote Link to comment https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/#findComment-931555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.