php automated setter and getter

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

I'm trying to implement some automated getter and setter for php objects.

My target is to automatically have for each properties the methods getProperty() and setProperty(value), that way if the method is not implemented for a property the script will simply set or get the value.

An example, to make myself clear:

class Foo {     public $Bar; }  $A = new A(); $A->setBar("bar"); $A->getBar(); // -> output "bar" 

or

class Foo {     public $Bar;     public function setBar($bar) { $Bar = $bar; }     public function getBar($bar) { return 'the value is: ' . $bar; } }  $A = new A(); $A->setBar("bar"); $A->getBar(); // -> output "the value is: bar" 

Any idea/hints on how to accomplish this?

回答1:

If you want to simulate the getXy and setXy functions for arbitrary properties, then use the magic __call wrapper:

function __call($method, $params) {       $var = lcfirst(substr($method, 3));       if (strncasecmp($method, "get", 3) === 0) {          return $this->$var;      }      if (strncasecmp($method, "set", 3) === 0) {          $this->$var = $params[0];      } } 

This would be a good opportunity to do something useful for once, by adding a typemap or anything. Otherwise eschewing getters and setters alltogether might be advisable.



回答2:

read the magic functions of php and your need you can use __get and __set functions

read this



回答3:

I know this question might be old, but I think I can help you or people that will came across this question in the future... There is a project/library called pLombok. It is the equivalent of Java's lombok (https://projectlombok.org/) where developers developed a library that generates code like getter and setter on the fly to get rid of boilerplate code. So the basic idea of my project is to use special comments like:

/** * @Setter */

/** * @Getter */

and so on to tell pLombok to generate the getter and setter for each property and add the generated code to the particular class before the program is executed.

Example

The following code:

namespace awesome\project; /**  * @Getter  * @Setter  */ class Person {   private $name;   private $age; } 

generates:

namespace awesome\project; class Person {   private $name;   private $age;    public function getName()  {     return $this->name;   }   public function getAge() {     return $this->age;   }   public function setName($name) {     $this->name = $name;   }   public function setAge($age) {     $this->age = $age;   } } 

on the fly!

There are many other features included like generating a no fields constructor, an all argument constructor or a simple toString method and so on.

Install with Composer

With composer you can install pLombok easily:

composer require watzerm/plombok:dev-master 

After that you can use it like this:

Create a simple class (src/project/Person.php)

namespace awesome\project; /**  * @Getter  * @Setter  * @NoArgsConstructor  */ class Person {   private $name;   private $age; } 

Configure autoloading for Composer (composer.json)

{     "require": {         "watzerm/plombok": "dev-master"     },     "autoload": {         "psr-4": {             "awesome\\": "src/"          }     } } 

Do a simple:

composer update 

Now tell pLombok to watch and care about the particular namespace, for that create a new file:

Use pLombok and the generated getter/setter (test.php)

include 'vendor/autoload.php';  use pLombok\Autoload\Autoload; use awesome\project\Person;  //Tell pLombok to consider that namespace Autoload::register('awesome\\project');  //Using getter and setter $person = new Person(); $person->setName('foobar'); echo $person->getName(), PHP_EOL; 

Documentation

The full documentation is placed on GitHub: https://github.com/watzerm/pLombok

I hope this helps.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!