Jump to content

Recommended Posts

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;
}


}

?>

Link to comment
https://forums.phpfreaks.com/topic/176691-properly-encapsulating-data/
Share on other sites

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

__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

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? 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.