DownloadPhpGenerator Element
This directory contains the basics. Any element contained by a PHP file should be here.
Using one of these elements, you can generate the content of a basic element:
-
simple php file
-
class: classic, abstract, interface
-
method
-
variable
-
function
-
property class
-
constant
-
annotation block
-
directive
Main features
Generate any basic PHP source code you want using a flexible PHP library
Create a directive
With one directive
$declare = new PhpDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1);
echo $declare->toString();
displays declare(strict_types=1);
With multiple directives
$declare = new PhpDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1);
$declare->addChild(new PhpDeclare(PhpDeclare::DIRECTIVE_TICKS, 1));
$declare->addChild(new PhpDeclare(PhpDeclare::DIRECTIVE_ENCODING, 'UTF-8'));
echo $declare->toString();
displays declare(strict_types=1, ticks=1, encoding='UTF-8');
Create a variable of any type
An integer
$variable = new PhpVariable('bar', 1);
echo $variable->toString();
displays $bar = 1;
A string
$variable = new PhpVariable('bar', '1');
echo $variable->toString();
displays $bar = '1';
An object
$variable = new PhpVariable('bar', 'new DOMDocument(\'1.0\', \'utf-8\')');
echo $variable->toString();
displays $bar = new DOMDocument('1.0', 'utf-8');
The result of a function
$variable = new PhpVariable('bar', 'is_array($foo)');
echo $variable->toString();
displays $bar = is_array($foo);
A class's constant
$variable = new PhpVariable('bar', 'stdClass::FOO');
echo $variable->toString();
displays $bar = stdClass::FOO;
A global constant
$variable = new PhpVariable('bar', '::XML_ELEMENT_NODE');
echo $variable->toString();
displays $bar = XML_ELEMENT_NODE;
Create a constant
As global
$constant = new PhpConstant('FOO', true);
echo $constant->toString();
displays define('FOO', true);
For a class
$constant = new PhpConstant('foo', true, new PhpClass('Bar'));
echo $constant->toString();
displays const FOO = true;
Create an annotation block
Simple
$annotationBlock = new PhpAnnotationBlock([
'This sample annotation is on one line',
]);
echo $annotationBlock->toString();
displays /
* This sample annotation is on one line
*/
More complex
$annotationBlock = new PhpAnnotationBlock();
$annotationBlock
->addChild(new PhpAnnotation('date', '2015-01-01'))
->addChild(new PhpAnnotation('author', 'PhpTeam'))
->addChild('This annotation is useful!');
echo $annotationBlock->toString();
displays /
* @date 2015-01-01
* @author PhpTeam
* This annotation is useful!
*/
Create a function
Simple function without any body
$function = new PhpFunction('foo', [
'bar',
[
'name' => 'demo',
'value' => 1,
],
[
'name' => 'sample',
'value' => null,
],
new PhpFunctionParameter('deamon', true),
]);
echo $function->toString();
displays function foo($bar, $demo = 1, $sample = NULL, $deamon = true)
{
}
Function with a body
$function = new PhpFunction('foo', [
'bar',
[
'name' => 'demo',
'value' => 1,
],
[
'name' => 'sample',
'value' => null,
],
new PhpFunctionParameter('deamon', true),
]);
$function
->addChild(new PhpVariable('bar', 1))
->addChild('return $bar;');
echo $function->toString();
displays function foo($bar, $demo = 1, $sample = NULL, $deamon = true)
{
$bar = 1;
return $bar;
}
Create a method
Simple public method without any body
$method = new PhpMethod('foo', [
'bar',
[
'name' => 'demo',
'value' => 1,
],
[
'name' => 'sample',
'value' => null,
],
new PhpFunctionParameter('deamon', true),
]);
echo $method->toString();
displays public function foo($bar, $demo = 1, $sample = NULL, $deamon = true)
{
}
Simple public method with a body
$method = new PhpMethod('foo', [
'bar',
[
'name' => 'demo',
'value' => 1,
],
[
'name' => 'sample',
'value' => null,
],
new PhpFunctionParameter('deamon', true),
]);
$method
->addChild(new PhpVariable('bar', 1))
->addChild('return $bar;');
echo $method->toString();
displays public function foo($bar, $demo = 1, $sample = NULL, $deamon = true)
{
$bar = 1;
return $bar;
}
Simple protected method without any body
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PROTECTED);
echo $method->toString();
displays protected function foo($bar)
{
}
Simple private method without any body
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PRIVATE);
echo $method->toString();
displays private function foo($bar)
{
}
Simple abstract public method without any body
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, true);
echo $method->toString();
displays abstract public function foo($bar);
Simple static public method without any body
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, true);
echo $method->toString();
displays public static function foo($bar)
{
}
Simple final public method without any body
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, false, true);
echo $method->toString();
displays final public function foo($bar)
{
}
Simple public method with no body asked
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, false, false, false);
echo $method->toString();
displays public function foo($bar);
Create a class, an abstract class
Simple class without any method
$class = new PhpClass('Foo');
echo $class->toString();
displays class Foo
{
}
Simple abstract class without any method
$class = new PhpClass('Foo', true);
echo $class->toString();
displays abstract class Foo
{
}
Simple class without any method with inheritance
$class = new PhpClass('Foo', false, 'Bar');
echo $class->toString();
displays class Foo extends Bar
{
}
Simple class without any method with implementation
$class = new PhpClass('Foo', false, 'Bar', [
'Demo',
'Sample',
]);
// equivalent to:
$class = new PhpClass('Foo', false, 'Bar', [
new PhpClass('Demo'),
new PhpClass('Sample'),
]);
echo $class->toString();
displays class Foo extends Bar implements Demo, Sample
{
}
Class with one empty method
$class = new PhpClass('Foo');
$class->addChild(new PhpMethod('bar'));
echo $class->toString();
displays class Foo
{
public function bar()
{
}
}
Class with one method
$class = new PhpClass('Foo');
$method = new PhpMethod('bar', [
'bar',
'foo',
'sample',
], PhpMethod::ACCESS_PRIVATE);
$method->addChild(new PhpVariable('foo', 1));
$class->addChild($method);
echo $class->toString();
displays class Foo
{
private function bar($bar, $foo, $sample)
{
$foo = 1;
}
}
Create an interface
Simple class without any method
$interface = new PhpInterface('Foo');
echo $interface->toString();
displays interface Foo
{
}
Simple class with one method
$interface = new PhpInterface('Foo');
$interface->addChild(new PhpMethod('bar'));
echo $interface->toString();
displays interface Foo
{
public function bar();
}
Interface does not accept any property
$interface = new PhpInterface('Foo');
$class->addChild(new PhpProperty('Bar'));
throws an ` \InvalidArgumentException` exception.
Generate a file from a simple file to a class file
Containing one variable
$file = new PhpFile('foo');
$file->addChild(new PhpVariable('foo', 1));
echo $file->toString();
displays <?php
$foo = 1;
Containing one constant
$file = new PhpFile('foo');
$file->addChild(new PhpConstant('foo', 1));
echo $file->toString();
displays <?php
define('foo', 1);
Containing one function
$file = new PhpFile('foo');
$file->addChild(new PhpFunction('foo', [
'foo',
'sample',
'demo',
]));
echo $file->toString();
displays <?php
function foo($foo, $sample, $demo)
{
}
Containing one annotation block
$file = new PhpFile('foo');
$file->addChild(new PhpAnnotationBlock([
'date is the key',
'time is the core key',
]));
echo $file->toString();
displays <?php
/
* date is the key
* time is the core key
*/
Containing an annotation block and a class
$file = new PhpFile('foo');
$file->addChild(new PhpDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1));
$file->addChild(new PhpAnnotationBlock([
'date is the key',
'time is the core key',
]));
$class = new PhpClass('Foo');
$class->addChild(new PhpMethod('Bar'));
$file->addChild($class);
echo $file->toString();
displays <?php
declare(strict_types=1);
/
* date is the key
* time is the core key
*/
class Foo
{
public function Bar()
{
}
}
Main constraints
Each element must only have access to its sub content, this means a class does not care of its annotations:
-
a file contains: directives, constants, variables, annotation blocks, empty string lines, classes, functions, interfaces
-
a class contains/an abstract class: constants, properties, methods, annotation blocks, empty string lines
-
an interface contains: constants, methods, annotation blocks, empty string lines
-
a method contains: variables, annotation blocks, string code lines, empty string lines
-
a function contains: variables, string code lines, empty line, annotation blocks
-
an annotation block contains: annotations
-
variable, property, function parameter, annotation and constant can't contain any element
|