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
$declare = new PhpDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1);
echo $declare->toString();
displays
declare(strict_types=1);
$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');
$variable = new PhpVariable('bar', 1);
echo $variable->toString();
displays
$bar = 1;
$variable = new PhpVariable('bar', '1');
echo $variable->toString();
displays
$bar = '1';
$variable = new PhpVariable('bar', 'new DOMDocument(\'1.0\', \'utf-8\')');
echo $variable->toString();
displays
$bar = new DOMDocument('1.0', 'utf-8');
$variable = new PhpVariable('bar', 'is_array($foo)');
echo $variable->toString();
displays
$bar = is_array($foo);
$variable = new PhpVariable('bar', 'stdClass::FOO');
echo $variable->toString();
displays
$bar = stdClass::FOO;
$variable = new PhpVariable('bar', '::XML_ELEMENT_NODE');
echo $variable->toString();
displays
$bar = XML_ELEMENT_NODE;
$constant = new PhpConstant('FOO', true);
echo $constant->toString();
displays
define('FOO', true);
$constant = new PhpConstant('foo', true, new PhpClass('Bar'));
echo $constant->toString();
displays
const FOO = true;
$annotationBlock = new PhpAnnotationBlock([
'This sample annotation is on one line',
]);
echo $annotationBlock->toString();
displays
/**
* This sample annotation is on one line
*/
$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!
*/
$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 = 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;
}
$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)
{
}
$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;
}
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PROTECTED);
echo $method->toString();
displays
protected function foo($bar)
{
}
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PRIVATE);
echo $method->toString();
displays
private function foo($bar)
{
}
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, true);
echo $method->toString();
displays
abstract public function foo($bar);
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, true);
echo $method->toString();
displays
public static function foo($bar)
{
}
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, false, true);
echo $method->toString();
displays
final public function foo($bar)
{
}
$method = new PhpMethod('foo', [
'bar',
], PhpMethod::ACCESS_PUBLIC, false, false, false, false);
echo $method->toString();
displays
public function foo($bar);
$class = new PhpClass('Foo');
echo $class->toString();
displays
class Foo
{
}
$class = new PhpClass('Foo', true);
echo $class->toString();
displays
abstract class Foo
{
}
$class = new PhpClass('Foo', false, 'Bar');
echo $class->toString();
displays
class Foo extends Bar
{
}
$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 = new PhpClass('Foo');
$class->addChild(new PhpMethod('bar'));
echo $class->toString();
displays
class Foo
{
public function bar()
{
}
}
$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;
}
}
$interface = new PhpInterface('Foo');
echo $interface->toString();
displays
interface Foo
{
}
$interface = new PhpInterface('Foo');
$interface->addChild(new PhpMethod('bar'));
echo $interface->toString();
displays
interface Foo
{
public function bar();
}
$interface = new PhpInterface('Foo');
$class->addChild(new PhpProperty('Bar'));
throws an \InvalidArgumentException
exception.
$file = new PhpFile('foo');
$file->addChild(new PhpVariable('foo', 1));
echo $file->toString();
displays
<?php
$foo = 1;
$file = new PhpFile('foo');
$file->addChild(new PhpConstant('foo', 1));
echo $file->toString();
displays
<?php
define('foo', 1);
$file = new PhpFile('foo');
$file->addChild(new PhpFunction('foo', [
'foo',
'sample',
'demo',
]));
echo $file->toString();
displays
<?php
function foo($foo, $sample, $demo)
{
}
$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
*/
$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()
{
}
}
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