Jump to content

Setters and getters


Pain

Recommended Posts

I have a question regarding setters and getters.

 

Why most people use a separate setter for each property?

 
public function setTitle($title) {
 
$this->title = $title;
 
}
 

public function setDescription($description) {
 
$this->description = $description;
 
}
 

public function setPostcode($postcode) {
 
$this->postcode = $postcode;
 
}
 

public function setPicture($picture) {
 
$this->picture = $picture;
 
}



 
public function getTitle() {
 
return $this->title;
 
}
 

public function getDescription() {
 
return $this->description;
 
}
 

public function getPostcode() {
 
return $this->postcode;
 
}
 

public function getPicture() {
 
return $this->picture;
 
}

Why not just do this?

public function setDetails($title, $description, $postcode, $picture) {
 
$this->title       = $title;
$this->description = $description; 
$this->postcode    = $postcode;
$this->picture     = $picture;
 
}
 
public function getTitle() {
 
return $this->title;
 
}
 

public function getDescription() {
 
return $this->description;
 
}
 

public function getPostcode() {
 
return $this->postcode;
 
}
 

public function getPicture() {
 
return $this->picture;
 
}



Edited by Pain
Link to comment
Share on other sites

you can do it that way if you want. Alternatively you can have your setter method take 2 arguments: 1 being the property to set, and 2 being the value to set it.

 

But the point of setter and getter methods is to not allow direct access to the property. If the only thing your setter method is going to do is this:

 

function setVar($var) {
  $this->var = $var;
}
If that's all you're going to do in your setter method, then there's no point in having a setter method at all. The point of a setter method is to validate the value so that the property only contains expected values.

 

For example, let's say you had $this->someNumber and it needs to be a number. But let's say some other code attempts to assign a string to it. And then let's say you have another piece of code that attempts to perform an operation based on that variable, and it breaks, because it expected a number, not a string. That's the point of setter method: to provide a level of validation and/or formatting to ensure that other pieces of code will get expected values.

 

So in practice, you'd do something like this:

 

function setSomeNumber($num) {
  if (ctype_digit($num))
    $this->someNumber = $num;
  else
    return false;
}
Actually what you'd really want to do is throw an exception instead of return false, but this is just demonstrating the principle that the setter function is there to validate and reject bad values, or otherwise guarantee that the property will always be what it's supposed to be. IOW it's pretty much the same concepts/principles as what you normally do with user input from form submits, just a slightly different context.
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.