RopeADope Posted March 1, 2011 Share Posted March 1, 2011 Hi all. I'm just getting my feet wet with OOPHP. My question is "why have a setter method when you can just use the __construct method to set everything?" and "would you need a separate setter method for each attribute of an object?"(i.e. set_first, set_last, set_gender, etc.) The code... <?php class person{ var $first; var $last; var $gender; function __construct($first,$last,$gender){ $this->first=$first; $this->last=$last; $this->gender=$gender; } function set_first($new_name){ $this->first=$new_name; } function get_person(){ return $this->first . $this->last . $this->gender; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229280-oophp-construct-instead-of-setter-method/ Share on other sites More sharing options...
.josh Posted March 1, 2011 Share Posted March 1, 2011 __construct() is used to automatically do stuff when an object is instantiated. Setter methods are used to set properties of a class/object. The point of setter methods is to not allow people to directly access object properties. It is used to ensure that they are only set with correct values/formats. For example: class person { var $age; } Let's say you have an $age property for your person object. You expect this to be an integer value in other methods used to do whatever based on age. As it stands in the example above, you can set it to whatever you want (like a string, array, another object, etc...) but that will break your other code that depends on it being an integer. In order to ensure that it will always be an integer, you create a setter method that validates the data before setting it. Basic example: class person { private $age; public function set_age($age) { if (!preg_match('~^[0-9]+$~',$age)) { // not an integer, throw an error/exception or do something else involving screaming at someone instead of setting $age } else { $this->age = $age; } } } If you do it this way, you can ensure that the class is encapsulated. You can ensure that if someone else is using your class, that passing something other than an integer for age will not break other things in your class. If it helps, you can think of it as the same principle as form validation / error handling for a form on a web page. Quote Link to comment https://forums.phpfreaks.com/topic/229280-oophp-construct-instead-of-setter-method/#findComment-1181448 Share on other sites More sharing options...
.josh Posted March 1, 2011 Share Posted March 1, 2011 p.s. - we like to refer to PHP Object Oriented Programming as POOP Quote Link to comment https://forums.phpfreaks.com/topic/229280-oophp-construct-instead-of-setter-method/#findComment-1181454 Share on other sites More sharing options...
.josh Posted March 1, 2011 Share Posted March 1, 2011 p.p.s - yes, you should be using your getter/setter methods even within your own class. So for instance, if you are passing some values during object instantiation to be set when the object is created, you should be calling the getter/setter methods from within your __construct(). This is also for ensuring that you are setting it w/ correct values/formats. It helps cut down the debug/qa time of development because it helps you track down bugs easier. For example, let's say you set $age directly in your construct but then later on you use it to do something like calculate birth year. PHP will happily accept a string for $age but when your calculate_birthyear() function goes to use it, it will mess up. At best, it might give you an incorrect value. At worst, it will cause a php error that may or may not stop the script. You of course could validate $age and make sure that it is an integer before trying to use it, in the calculate_birthyear() function example, but let's say you have 10 functions that use it - you would have to validate $age in all of them. With the setter method, it's validated in one place, so you don't have to worry about it being the correct value/format in your other functions. Quote Link to comment https://forums.phpfreaks.com/topic/229280-oophp-construct-instead-of-setter-method/#findComment-1181455 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.