PHP code example of leaditin / code

1. Go to this page and download the library: Download leaditin/code library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leaditin / code example snippets




use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Import;
use Leaditin\Code\Member\Constant;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Member\Property;
use Leaditin\Code\Tag;
use Leaditin\Code\Type;
use Leaditin\Code\Value;
use Leaditin\Code\Visibility;

$generator = (new Factory())->classGenerator();
$generator
    ->setName('MyClass')
    ->setNamespace('MyDummyNamespace')
    ->setExtends('MyDummyClass')
    ->implementInterface('MyDummyInterface')
    ->useTrait('MyDummyTrait')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description',
            [
                new Tag('author', 'author@domain')
            ]
        )
    )
    ->addImport(new Import('MyNamespace\\MyDummyTrait'))
    ->addConstant(new Constant('CONST_A', 2, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addConstant(new Constant('CONST_B', 3, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addProperty(new Property('name', new Value('Some User'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addProperty(new Property('email', new Value('author@domain'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->name;', null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->email;', null, new Type(Type::TYPE_STRING)));

echo $generator->generate();



namespace MyDummyNamespace;

use MyNamespace\MyDummyTrait;

/**
 * Short description
 *
 * Long description
 *
 * @author author@domain
 */
class MyClass extends MyDummyClass implements MyDummyInterface
{
    use MyDummyTrait;

    public const CONST_A = 2;
    public const CONST_B = 3;

    /**
     * @var string
     */
    protected $name = 'Some User';

    /**
     * @var string
     */
    protected $email = 'author@domain';

    /**
     * @return string
     */
    public function name(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function email(): string
    {
        return $this->email;
    }
}



use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Member\Constant;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Type;
use Leaditin\Code\Visibility;

$generator = (new Factory())->interfaceGenerator();
$generator
    ->setName('MyInterface')
    ->setNamespace('MyDummyNamespace')
    ->setExtends('MyDummyInterface')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description'
        )
    )
    ->addConstant(new Constant('CONST_A', 2, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addConstant(new Constant('CONST_B', 3, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, null, null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, null, null, new Type(Type::TYPE_STRING)));

echo $generator->generate();




namespace MyDummyNamespace;

/**
 * Short description
 *
 * Long description
 */
interface MyInterface extends MyDummyInterface
{
    public const CONST_A = 2;
    public const CONST_B = 3;

    /**
     * @return string
     */
    public function name(): string;

    /**
     * @return string
     */
    public function email(): string;
}



use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Member\Property;
use Leaditin\Code\Type;
use Leaditin\Code\Value;

$generator = (new Factory())->traitGenerator();
$generator
    ->setName('MyTrait')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description'
        )
    )
    ->addProperty(new Property('name', new Value('Some User'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addProperty(new Property('email', new Value('author@domain'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->name;', null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->email;', null, new Type(Type::TYPE_STRING)));

echo $generator->generate();



/**
 * Short description
 *
 * Long description
 */
trait MyTrait
{
    /**
     * @var string
     */
    protected $name = 'Some User';

    /**
     * @var string
     */
    protected $email = 'author@domain';

    /**
     * @return string
     */
    public function name(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function email(): string
    {
        return $this->email;
    }
}