Jump to content

Collective Getter's and Setter's


LTGoldman

Recommended Posts

After instruction from Stefan's response to my question on the Killersites.com web-form; I am opening this thread for the sites community to 'jump in' and perhaps for other beginners to benefit from responses to the questions I ask:

 

After watching Stefan's OOP: PHP videos about classes and objects I have the following questions:

 

When you have a class which has a fairly large amount of properties (variables) to have a getter or setter method for each of your properties could become very tedious is it OK to create a getter or setter method which collectively works on a group of properties and if so is it OK to break with the convention of naming your getters and setters to accommodate the collective manipulation of class properties? i.e:

 


class person {

 //Class Properties

 $height;
 $weight;
 $age;

 //Class Methods

 public set_properties($h,$w,$a)  //  Instead of a setter method for each e.g. set_height, set_weight etc............
 {
   $this->height = $h;
   $this->weight = $w;
   $this->age = $a;
 }

}

 

Secondly in Stefan's PHP Videos he declares variables using var:

 

var $variable1;

 

Is the var absolutely necessary as I've seen variables without the var keyword and it seems to work OK.

Link to comment
Share on other sites

I don't really see any problem with using a set_properties() function, unless you need to set those properties separately from one another (which could happen?)

 

According to http://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do, using "var" is only for PHP4, and it has been deprecated in PHP5. Assuming you are running PHP5, it is no longer needed. You will, however, want to use public/private/protected (see the previous link and/or http://www.php.net/manual/en/language.oop5.visibility.php).

Link to comment
Share on other sites

You do realize that that would be an immense pain in the ass if you got more than 10 properties that you need to keep track on, not only the names.. but also the order of all of them in the parameters. For large scripts it would be a pain in the ass.

Not to mention how the function code would grow in the pace of new properties introduced which would introduce a time complexity that varied depending on the number of properties.

 

I'd suggest you solve it using dynamic variables instead, that wasy you can create generic (not true generic but still generic in a sense) way. I had something similar back when I was taking the PHP certificate and I think I got some bitching for doing it in this fashion, but hey as long as it's valid it's valid, and if you feel as it will make your code look/feel/behave better than check this out:

 

(btw, in your code example you haven't defined what accessibility level your properties have, thus technically you could access them straight by doing $reference->nameOfVairable without any need of getters and setters. However I assume you meant them being private as you got a set function )

 

<?php
/**
* Forum example @ killersites.com
* 2010 
*
* Exempel:  PHPClass
* @author Kraxzy
*/
class PHPClass {

 private $height = "short";
 private $weight = "skinny";
 private $age = "young"; 

 public function universalSetter($varName, $value){
     $this->$varName = $value;

 }
 public function universalGetter($varName){
     return $this->$varName;
 }
}
//Test
$t = new PHPClass();

echo $t->universalGetter(($name = "height"))."<br>";
echo $t->universalGetter(($name = "weight"))."<br>";
echo $t->universalGetter(($name = "age"))."<br>";

$t->universalSetter(($name = "height"), "tall");
$t->universalSetter(($name = "weight"), "heavy");
$t->universalSetter(($name = "age"), "old");

echo $t->universalGetter(($name = "height"))."<br>";
echo $t->universalGetter(($name = "weight"))."<br>";
echo $t->universalGetter(($name = "age"))."<br>";
?>

 

 

this will output:

 

short
skinny
young
tall
heavy
old

 

Thus it's changed and works.

Voilà!

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...