PHP Classes
elePHPant
Icontem

File: src/Element/README.md

Recommend this page to a friend!
  Classes of WsdlToPhp  >  PHP Code Generator  >  src/Element/README.md  >  Download  
File: src/Element/README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Code Generator
Generate PHP code elements programatically
Author: By
Last change: Text files should end with a newline character
Date: 1 year ago
Size: 9,816 bytes
 

Contents

Class file image Download

WsdlToPhp Php Generator, 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

Main features

Generate any basic PHP source code you want using a flexible PHP library

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(array(
    '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', array(
    'bar',
    array(
        'name' => 'demo',
        'value' => 1,
    ),
    array(
        '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', array(
    'bar',
    array(
        'name' => 'demo',
        'value' => 1,
    ),
    array(
        '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', array(
    'bar',
    array(
        'name' => 'demo',
        'value' => 1,
    ),
    array(
        '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', array(
    'bar',
    array(
        'name' => 'demo',
        'value' => 1,
    ),
    array(
        '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', array(
    'bar',
), PhpMethod::ACCESS_PROTECTED);
echo $method->toString();

displays

protected function foo($bar)
{
}
Simple private method without any body
$method = new PhpMethod('foo', array(
    'bar',
), PhpMethod::ACCESS_PRIVATE);
echo $method->toString();

displays

private function foo($bar)
{
}
Simple abstract public method without any body
$method = new PhpMethod('foo', array(
    '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', array(
    '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', array(
    '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', array(
    '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', array(
    'Demo',
    'Sample',
));
// equivalent to:
$class = new PhpClass('Foo', false, 'Bar', array(
    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', array(
    '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 `\InvaliddArgumentException` 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', array(
    '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(array(
    '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 PhpAnnotationBlock(array(
    '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
/
 * 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: 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