Generate Getters for PHP

I'm confused about PhpStorm's generation of Getters and Setters.

For example, if I have a class variable called $_token, and tell PhpStorm to generate a getter, it produces the following code:


public function getToken()
{
     return $this->_token;
}


What confuses me is that, simply, this is not a PHP getter. It's just a PHP function which happens to be called getToken(), and returns the value of $_token. It cannot be called as $obj->token, like a proper "getter" would.

A proper "getter" for $_token would look like this:

     public function __get($name)
  {
          switch($name)
          {
               case "token":
                    return $this->_token;
          }


          throw new InvalidArgumentException('Invalid property: $name.');
     }



Which would of course allow for $obj->token.

Why would PhpStorm generate the former, and not the latter?

(Reference: http://php.net/manual/en/language.oop5.overloading.php)
0
1 comment
Avatar
Permanently deleted user

Generating getters and setters is for generating methods.
Getters and setters also does not insist on meaning magic methods.

Its purpose is for generating methods for stuff like database models, child methods of say a factory, etc...
Where you are specifically using properties to generate methods.

Note: you can alter these under Settings -> File Templates -> PHP Class -> Code tab
I don't think there is a way to loop through the properties to generate code inside a method, as it wants to create methods out of the properties.
But like I said, its purpose is meant for generating methods.

0

Please sign in to leave a comment.