views:

444

answers:

7

I regularly create a class that has a few private variables. When an instance of this class is set, it should be possible to fill all variables of the class with getters and setters.

Is there an easy way to do this without having to type all these setters and getters for each variable on creation of the class?

now i have to type this for each var in the class

public function setVar($var){
$this->var = $var;
}

and

public function getVar(){
return $this->var;
}

We now use RegExp, but i don't know if this is the easiest way... We use Eclipse as environment, so perhaps there are some usefull plugins?

+4  A: 

Not the biggest fan of getters and setters. It's redundant, space wasting code.

There shouldn't be any reason to have a getter and setter for every private member in the class, they should be set and the values retrieved using the actual methods that the class was built for.

Evernoob
Haha! are you even a software developer?
Shiva
Sure, there are reasons that get/set methods might not be entirely necessary for every property, but there are really good reasons to have them. Proper use of them is a huge component of OO programming. To dismiss them as a waste of space shows disregard for the poster's intentions. And doesn't even begin the answer the question.
Bryan M.
It seems we have a disagreement here.
Chris
+2  A: 

Have you looked at the __set and __get methods? Don't know if this is what you mean but the are automatically called whenever a class member is SET or Retrieved/Fetched/Accessed.

andreas
A: 

Maybe better sollution:

class base {

    protected $_vars = array();


    public function setVar($name, $value = null) {
        $this->_vars[$name] = $value;
    }

    public function getVar($name) {
        return isset($this->_vars[$name]) ? $this->_vars[$name] : null;
    }
}

And simply extend this class. Or also you can use __set and __get methods, but they are quite slower.

Pawka
this might be a good solution, but is it a good idea to put all in an array, and not all as different vars? or doesn't that matter?
Kennethvr
If it is working data (like arrays, strings, etc.) it's OK. But if you want to set some objects like database object or others, you shouldn't do this way. Then use constructors or other ways.
Pawka
Are you sure about __set and __get mehods being slow??
andho
@andho Yes, I'm pretty sure. Sorry, I can't find the article about how PHP works with memory when using magic methods (I've read before a while), but if You search, there are written almost on every list of "PHP performance tips" that magic methods can slow down your code.
Pawka
+1  A: 

Zend Studio has a feature of automatic getter/setter generation.

FractalizeR
A: 

Just seen this [Potentially malicious site link removed] offering a great PHP setter and getter tool. What I like about this one, is that its easy and straight forward in generating the source code.

PHP Rocker
I'm going on the assumption that you were trying to help and not trying to infect people's computers. For now.
Will
A: 

I think the best way is to use the __set and __get with some string functions, Here let me show you.

class UserProfile
{
    public function __get($key)
    {
        if(isset($this->$key))
        {
            return $this->$key;
        }
    }

    public function __set($key,$val)
    {
        $this->cahnge($key,$val);
    }

    public function __call($key,$params)
    {
        if(substr("set",$key))
        {
            //Update
        }elseif(substr("get",$key))
        {
            //return
        }

        //Else Blah
    }

    private function change($key,$val)
    {
        if(isset($this->$key))
        {
            $this->$key = $val;
        }
    }
}

the __call() method will allow you to set with functions such as

$profile->setUsername('Robert Pitt'); as long as you substr the set/get and check for the rest of the string as a value of the class :)

another example

public function __call($method,$params = array())
{

    if(isset($params[0]) && is_string($params[0]))
    {
        $this->$method = $params[0];
    }
}

//....

$profile->username('Robert Pitt');

Theres more work to be done here

RobertPitt
+1  A: 

With Eclipse go to Preferences -> PHP -> Editor -> Templates -> New and use something like this:

private $$${PropertyName};
${cursor}    
public function get${PropertyName}() 
{
  return $$this->${PropertyName};
}

public function set${PropertyName}($$value) 
{
  $$this->${PropertyName} = $$value;
}

To use the template type it's name and press ctrl+space - a context menu should also automatically appear when you type the name.

erisco