Go Back   CodeCall Programming Forum > Software Development > General Programming
Register Blogs Search Today's Posts Mark Forums Read

General Programming Non language specific and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-2009, 09:49 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Constructor vs. Setters for Instance Variables

I've seen a lot of both and depending on who the developer is, either way may be the preferred method. As far as I can tell, there is no standard method. I'm talking about the way instance variable values are set when the class is created.

Constructor Method:
PHP Code:
public function __construct string $dsn,  string $usernamestring $password) {
    
$this->__dsn         $dsn;
    
$this->__username $username;
    
$this->__password $password;


Setter Method:
PHP Code:
public function setDsn($dns) {
    
$this->__dsn         $dsn;
}

public function 
setUsername($username) {
    
$this->__username $username;
}

public function 
setPassword($password) {
    
$this->__password $password;

I prefer passing variables to the constructor. It is easier to read and less lines of code for both the object developer and the person creating the object.

I understand there may be some instances where having it passed to the constructor and using the same setters may be beneficial, such as when you want to allow data to be changed, but for this discussion I am strictly speaking of setting the data when the class is first created.

Which method do you prefer? Why?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-30-2009, 11:57 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Constructor vs. Setters for Instance Variables

Constructors, but I think that's because they're easy to work with in C++. Especially for some types of data, there can be a serious performance hit if you use a constructor to create something large, and then redefine it with setters.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-30-2009, 03:39 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Constructor vs. Setters for Instance Variables

I prefer a third way, hah. In C# it is called Object Initializers. You do not have to create a constructor (default one will do) and you do not need to create methods (or property accessors). Just use a default constructor and set whatever properties you want.

Here is some C# example.

PHP Code:
class Program
{
    class 
PersonalInfo
    
{
        public 
string FullName;
        public 
DateTime DateOfBirth;
        public 
int Age;
    }

    static 
void Main(string[] args)
    {
        
PersonalInfo me = new PersonalInfo()
        {
            
FullName "Arek Bulski",
            
DateOfBirth = new DateTime(20090102),
            
Age 21,
        };

        
/// This is me. No constructor required. Neither 
        /// any method or property set accessor. :)
    
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-30-2009, 03:42 PM
marwex89's Avatar
Code Warrior
 
Join Date: Jul 2008
Location: Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
Posts: 9,849
marwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of light
Send a message via AIM to marwex89 Send a message via MSN to marwex89
Re: Constructor vs. Setters for Instance Variables

One vote for constructors..

Posted via CodeCall Mobile
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
classes, constructor, created, objects, setters



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing variables in 2 scripts Perogy JavaScript and CSS 1 06-17-2009 07:27 PM
Variables in Visual Basic AlanQ Visual Basic Programming 1 04-20-2009 03:29 PM
Global Variables vs Data Duplicates scc General Programming 4 07-28-2008 09:16 PM
Variables clookid PHP Tutorials 1 01-11-2007 09:40 PM


All times are GMT -5. The time now is 09:58 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0