PHPackages                             rougin/classidy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rougin/classidy

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rougin/classidy
===============

Create PHP classes using PHP.

v0.1.1(1y ago)01.7k2MITPHPPHP &gt;=5.3.0CI passing

Since Oct 13Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/rougin/classidy)[ Packagist](https://packagist.org/packages/rougin/classidy)[ Docs](https://roug.in/classidy/)[ RSS](/packages/rougin-classidy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (2)

Classidy
========

[](#classidy)

[![Latest Version on Packagist](https://camo.githubusercontent.com/66107b2ad26cce36736232e2f4d55fbc64f4d89e28a987d27380be0b023ed9b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f636c6173736964792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/classidy)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/classidy/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/59b0a7ba4f52440eac63799e1326293a7c9210140c40508e90a13a2df40894a9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f636c6173736964792f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/classidy/actions)[![Coverage Status](https://camo.githubusercontent.com/b83dd666793b707115e12d7b0e4d96706af9cbf84563282b32e43d5dc7445bf7/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f636c6173736964793f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/classidy)[![Total Downloads](https://camo.githubusercontent.com/fdbc4844862104c69cbfdfb30e2d25d3cab01bbbf7cdcece780ea9a32ad8ef74/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f636c6173736964792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/classidy)

A package that creates PHP classes using PHP. That's it.

```
use Rougin\Classidy\Classidy;
use Rougin\Classidy\Generator;
use Rougin\Classidy\Method;

$class = new Classidy;

$class->setName('Hello');

$method = new Method('hi');

$method->setCodeEval(function ()
{
    return 'Hello world!';
});

$class->addMethod($method);

$maker = new Generator;

echo $maker->make($class);
```

Installation
------------

[](#installation)

Install `Classidy` through [Composer](https://getcomposer.org/):

```
$ composer require rougin/classidy
```

Basic usage
-----------

[](#basic-usage)

### Creating a simple class

[](#creating-a-simple-class)

Creating 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

setCodeLine(function ($lines) use ($shout)
{
    if ($shout)
    {
        $lines[] = "return 'HELLO WORLD!';";
    }
    else
    {
        $lines[] = "return 'Hello world!';";
    }

    return $lines;
});

// ...
```

### Adding parent class, interfaces

[](#adding-parent-class-interfaces)

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

addInterface(Greetable::class);
$class->addInterface(Helloable::class);

// ...
// ------------------------------------

// ...
```

```
$ php index.php

addTrait(Traitable::class);

// ...
// -----------------------------------

// ...
```

```
$ php index.php

setCodeEval(function ()
{
    return 'Hello world!';
});
$class->addMethod($method);
// -------------------------------------

// ...
```

To add arguments in a specified method, kindy use the following methods below:

MethodDescription`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

addClassArgument('test', 'Acme\Test')
    ->withoutTypeDeclared();
$method->setReturn('string');
$method->setCodeEval(function ($test)
{
    return $test->hello();
});
$class->addMethod($method);

// ...
```

```
$ php index.php

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

addStringProperty('text')
    ->withDefaultValue('Hello world!');

// ...

// Modify "greet" method to access "text" property ---
$method->setCodeEval(function ()
{
    return $this->text;
});
// ---------------------------------------------------

// ...
```

```
$ php index.php
