A package that creates PHP classes using PHP. That's it.
Install Classidy through Composer:
$ composer require rougin/classidyCreating a PHP class only requires the Classidy and Generator classes:
// index.php
use Rougin\Classidy\Classidy;
use Rougin\Classidy\Generator;
use Rougin\Classidy\Method;
// ...
// Create a new class definition ---
$class = new Classidy;
// ---------------------------------
// Define the details of the class ------------
$class->setComment('Sample class for Acme.');
$class->setNamespace('Acme');
$class->setPackage('Acme');
$class->setAuthor('John Doe', 'jdoe@acme.com');
$class->setName('Greet');
// --------------------------------------------
// Add a "greet" method in the class ---
$method = new Method('greet');
$method->setCodeEval(function ()
{
return 'Hello world!';
});
$class->addMethod($method);
// -------------------------------------
// Generate the class --------
$generator = new Generator;
echo $generator->make($class);
// ---------------------------$ php index.php
<?php
namespace Acme;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet
{
public function greet()
{
return 'Hello world!';
}
}The setCodeLine method can also be used for specifying code of a method in a line based format. This may be useful in adding conditions in generating code of a method:
// index.php
// ...
$shout = false;
$method->setCodeLine(function ($lines) use ($shout)
{
if ($shout)
{
$lines[] = "return 'HELLO WORLD!';";
}
else
{
$lines[] = "return 'Hello world!';";
}
return $lines;
});
// ...The class can be added with a parent class using extendsTo:
// index.php
use Acme\Hello\Greeter;
// ...
// Define the details of the class ---
// ...
$class->extendsTo(Greeter::class);
// ...
// -----------------------------------
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter
{
public function greet()
{
return 'Hello world!';
}
}Note
If the added parent class or interface is not from the same namespace of the class to be generated, Classidy will automatically import the said parent class/interface.
For adding interfaces, the addInterface method can be used:
// index.php
use Acme\Greetable;
use Acme\Helloable;
// ...
// Define the details of the class ----
// ...
$class->addInterface(Greetable::class);
$class->addInterface(Helloable::class);
// ...
// ------------------------------------
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
public function greet()
{
return 'Hello world!';
}
}Similar in defining class and interfaces, adding a trait is possible using addTrait:
// index.php
use Acme\Hello\Traitable;
// ...
// Define the details of the class ---
// ...
$class->addTrait(Traitable::class);
// ...
// -----------------------------------
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Traitable;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet
{
use Traitable;
public function greet()
{
return 'Hello world!';
}
}Based from the first example, the addMethod can be used to add a method to the class:
// index.php
// ...
// Add a "greet" method in the class ---
$method = new Method('greet');
$method->setCodeEval(function ()
{
return 'Hello world!';
});
$class->addMethod($method);
// -------------------------------------
// ...To add arguments in a specified method, kindy use the following methods below:
| Method | Description |
|---|---|
addArrayArgument |
Adds a property with a array as its data type. |
addBooleanArgument |
Adds an argument with a boolean as its data type. |
addClassArgument |
Adds an argument with the specified class. |
addFloatArgument |
Adds an argument with a float as its data type. |
addIntegerArgument |
Adds an argument with an integer as its data type. |
addStringArgument |
Adds an argument with a string as its data type. |
// index.php
// ...
$method = new Method('greet');
$method->addBooleanArgument('shout')
->withDefaultValue(false);
$method->setReturn('string');
$method->setCodeEval(function ()
{
return 'Hello world!';
});
$class->addMethod($method);
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
/**
* @param boolean $shout
*
* @return string
*/
public function greet($shout = false)
{
return 'Hello world!';
}
}To add a class argument without being its type declared, add withoutTypeDeclared after addClassArgument:
// index.php
// ...
$method = new Method('greet');
$method->addClassArgument('test', 'Acme\Test')
->withoutTypeDeclared();
$method->setReturn('string');
$method->setCodeEval(function ($test)
{
return $test->hello();
});
$class->addMethod($method);
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
/**
* @param \Acme\Test $test
*
* @return string
*/
public function greet($test)
{
return $test->hello();
}
}A method can also be defined as protected or private:
// index.php
// ...
$method = new Method('greet');
// Set the method as "protected" ---
$method->asProtected();
// ---------------------------------
// Set the method as "private" ---
$method->asPrivate();
// -------------------------------
// ...Note
By default, all of the specified methods are in public visibility.
The method can be alternatively be specified as a @method tag in the class:
// index.php
// ...
$method = new Method('greet');
// ...
// Set as "@method" in the class ---
$method->asTag();
// ---------------------------------
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @method string greet(boolean $shout = false)
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
}Similiar to adding arguments in a method, adding properties to a class can be done by the following:
| Method | Description |
|---|---|
addArrayProperty |
Adds a property with a array as its data type. |
addBooleanProperty |
Adds a property with a boolean as its data type. |
addClassProperty |
Adds a property with the specified class. |
addFloatProperty |
Adds a property with a float as its data type. |
addIntegerProperty |
Adds a property with an integer as its data type. |
addStringProperty |
Adds a property with a string as its data type. |
// index.php
// ...
$class->addStringProperty('text')
->withDefaultValue('Hello world!');
// ...
// Modify "greet" method to access "text" property ---
$method->setCodeEval(function ()
{
return $this->text;
});
// ---------------------------------------------------
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
/**
* @var string
*/
protected $text = 'Hello world!';
/**
* @param boolean $shout
*
* @return string
*/
public function greet($shout = false)
{
return $this->text;
}
}To change a visibility of a property, the methods asPublic and asPrivate can be used:
// index.php
// ...
$class->addStringProperty('text')
->asPrivate()
->withDefaultValue('Hello world!');
// ...Note
By default, all of the specified properties are in protected visibility.
Alternatively, the property be specified as a @property tag in the class:
// index.php
// ...
$class->addStringProperty('text')->asTag();
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* @property string $text
*
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
}The setEmpty method can be used to clear any methods and properties previously specified. This maybe useful when generating empty classes without specifying a new Classidy class:
// index.php
// ...
$class->setEmpty();
// ...$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/**
* Sample class for Acme.
*
* @package Acme
*
* @author John Doe <jdoe@acme.com>
*/
class Greet extends Greeter implements Greetable, Helloable
{
}Please see CHANGELOG for more information what has changed recently.
$ composer testThe MIT License (MIT). Please see LICENSE for more information.