Jump to content

OOPHP construct instead of setter method


RopeADope

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.