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; 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; 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.