Download the PHP package kanel/phpclasseditor without Composer
On this page you can find all versions of the php package kanel/phpclasseditor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kanel/phpclasseditor
More information about kanel/phpclasseditor
Files in kanel/phpclasseditor
Package phpclasseditor
Short Description Edit php files / classes by dynamically adding code to them
License
Informations about the package phpclasseditor
phpClassEditor
This library allows you to dynamically edit php class files by injecting code into them
How it works
Important : unless you call the save method, the file won't be changed Each function that edits the php class file return the new content of the file (after adding properties/methods)
1 Indentation
Before you create any property or method, you need to define what indentation to use.
Default one is 4 spaces.
If you want to change the default behaviour :
The example above means you want the default indentation to be 1 tab.
You can only use :
2 Adding properties to a class
You can inject properties into a php class file.
The example above adds three properties to the class
2.1 Property class
the "addProperty" method takes in a Property class.
A Property class must have at least a name (mandatory), defined in the construct
This will print in the class :
You can also set a "visibility" to your property in the construct (optional)
All the visibilities are defined in the Visibility class :
You can also define the property's default value (optional too)
will print
You can write whatever value you want (as string), or use the ones predefined in the Value class:
Finally, the last construct parameter allows to choose if the property is static (true) or not (false):
will print
All the parameters in the constuct (except the name) have designated setters, you can use:
will print :
3 Adding methods to a class
You can inject methods into a php class file.
The example above adds two methods to the class
The name and the visibility are both mandatory in the construct
All the visibilities are defined in the Visibility class :
You can define multiple things using the Method class : is it static? abstract? final ? does it have a return type? a doc comment? parameters ?
Since a method can not be both final and abtract, it is the last one set to true that is taken.
so this will print:
Note the the return type can be anything you send, you can also use the consts defined in Type class
You can also add parameters to your method. The Method class has an "addParameter" function that allows to you add as many parameters as needed. These are all the possible parameters classes you can use :
Each one of these parameters classes takes in a mandatory name in their construct
They also take in a default value as second parameter (except for ClassParameter, see below) when there is one
You can use the predefined ones or add yours
The last parameter is a boolean that tells if the parameter is a splat or not A splat is the ... annotation that allows to send multiple parameters
Please note also that each parameter
The ClassParameter has a slightly different construct as it takes as a second parameter the full class name
This will print :
You must have noted that params and return type will always generate a docComment