Automatic php and getter setter

I am trying to implement some automatic getter and setter for php objects.

My goal is to automatically use the getProperty() and setProperty(value) methods for each property, so if the method is not implemented for the property, the script will simply set or get a value.

Example to make sure:

 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 / tips on how to do this?

+7
source share
3 answers

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

 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 at a time by adding a sample card or something else. Otherwise, getters and setters could be recommended.

+19
source

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

read it

+3
source

I know this question may be old, but I think I can help you or people who will face this question in the future ... There is a project / library called pLombok. This is the equivalent of Java lombok ( https://projectlombok.org/ ), where developers have developed a library that generates code on the fly, such as getter and setter, to get rid of the template code. Therefore, the main idea of ​​my project is to use special comments, for example:

/** * @Setter */

/** * @Getter */

etc., to tell pLombok to generate getter and setter for each property and add the generated code to a specific 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 functions, such as creating a borderless constructor, a constructor of all arguments, or a simple toString method, etc.

Install using Composer

With composer, you can easily install pLombok:

 composer require watzerm/plombok:dev-master 

After that, you can use it as follows:

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

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

Setting up startup for Composer (composer.json)

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

Make it simple:

 composer update 

Now tell pLombok to look and take care of a specific namespace, for which 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

Full documentation is posted on GitHub: https://github.com/watzerm/pLombok

Hope this helps.

0
source

All Articles