Pain Posted February 9, 2014 Share Posted February 9, 2014 (edited) 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 February 9, 2014 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/286071-setters-and-getters/ Share on other sites More sharing options...
.josh Posted February 9, 2014 Share Posted February 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286071-setters-and-getters/#findComment-1468323 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.