|
||||||
| General Programming Non language specific and anything not covered in other topics. Talk about Programming Theory here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
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:
Setter Method: PHP Code:
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?
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
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 |
|
||||
|
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:
__________________
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions |
![]() |
| Tags |
| classes, constructor, created, objects, setters |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| 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.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc