{"id":16423579,"url":"https://github.com/rougin/classidy","last_synced_at":"2025-10-26T22:31:40.955Z","repository":{"id":256240690,"uuid":"854687034","full_name":"rougin/classidy","owner":"rougin","description":"Create PHP classes using PHP.","archived":false,"fork":false,"pushed_at":"2024-10-22T01:07:11.000Z","size":55,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T00:51:06.586Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rougin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-09T15:54:02.000Z","updated_at":"2024-10-22T01:07:14.000Z","dependencies_parsed_at":"2024-09-16T09:54:24.240Z","dependency_job_id":"62d85d1e-0bcd-4e72-9985-cf34fc2dde16","html_url":"https://github.com/rougin/classidy","commit_stats":null,"previous_names":["rougin/classidy"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fclassidy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fclassidy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fclassidy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fclassidy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rougin","download_url":"https://codeload.github.com/rougin/classidy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238408487,"owners_count":19467099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T07:40:20.424Z","updated_at":"2025-10-26T22:31:40.637Z","avatar_url":"https://github.com/rougin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Classidy\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]][link-license]\n[![Build Status][ico-build]][link-build]\n[![Coverage Status][ico-coverage]][link-coverage]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nA package that creates PHP classes using PHP. That's it.\n\n## Installation\n\nInstall `Classidy` through [Composer](https://getcomposer.org/):\n\n``` bash\n$ composer require rougin/classidy\n```\n\n## Basic Usage\n\n### Creating a simple class\n\nCreating a PHP class only requires the `Classidy` and `Generator` classes:\n\n``` php\n// index.php\n\nuse Rougin\\Classidy\\Classidy;\nuse Rougin\\Classidy\\Generator;\nuse Rougin\\Classidy\\Method;\n\n// ...\n\n// Create a new class definition ---\n$class = new Classidy;\n// ---------------------------------\n\n// Define the details of the class ------------\n$class-\u003esetComment('Sample class for Acme.');\n$class-\u003esetNamespace('Acme');\n$class-\u003esetPackage('Acme');\n$class-\u003esetAuthor('John Doe', 'jdoe@acme.com');\n$class-\u003esetName('Greet');\n// --------------------------------------------\n\n// Add a \"greet\" method in the class ---\n$method = new Method('greet');\n$method-\u003esetCodeEval(function ()\n{\n    return 'Hello world!';\n});\n$class-\u003eaddMethod($method);\n// -------------------------------------\n\n// Generate the class --------\n$generator = new Generator;\n\necho $generator-\u003emake($class);\n// ---------------------------\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet\n{\n    public function greet()\n    {\n        return 'Hello world!';\n    }\n}\n```\n\nThe `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:\n\n``` php\n// index.php\n\n// ...\n\n$shout = false;\n\n$method-\u003esetCodeLine(function ($lines) use ($shout)\n{\n    if ($shout)\n    {\n        $lines[] = \"return 'HELLO WORLD!';\";\n    }\n    else\n    {\n        $lines[] = \"return 'Hello world!';\";\n    }\n\n    return $lines;\n});\n\n// ...\n```\n\n### Adding parent class, interfaces\n\nThe class can be added with a parent class using `extendsTo`:\n\n``` php\n// index.php\n\nuse Acme\\Hello\\Greeter;\n\n// ...\n\n// Define the details of the class ---\n// ...\n\n$class-\u003eextendsTo(Greeter::class);\n\n// ...\n// -----------------------------------\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter\n{\n    public function greet()\n    {\n        return 'Hello world!';\n    }\n}\n```\n\n\u003e [!NOTE]\n\u003e 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.\n\nFor adding interfaces, the `addInterface` method can be used:\n\n``` php\n// index.php\n\nuse Acme\\Greetable;\nuse Acme\\Helloable;\n\n// ...\n\n// Define the details of the class ----\n// ...\n\n$class-\u003eaddInterface(Greetable::class);\n$class-\u003eaddInterface(Helloable::class);\n\n// ...\n// ------------------------------------\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n    public function greet()\n    {\n        return 'Hello world!';\n    }\n}\n```\n\n### Adding traits\n\nSimilar in defining class and interfaces, adding a trait is possible using `addTrait`:\n\n``` php\n// index.php\n\nuse Acme\\Hello\\Traitable;\n\n// ...\n\n// Define the details of the class ---\n// ...\n\n$class-\u003eaddTrait(Traitable::class);\n\n// ...\n// -----------------------------------\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Traitable;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet\n{\n    use Traitable;\n\n    public function greet()\n    {\n        return 'Hello world!';\n    }\n}\n```\n\n### Adding methods\n\nBased from the first example, the `addMethod` can be used to add a method to the class:\n\n``` php\n// index.php\n\n// ...\n\n// Add a \"greet\" method in the class ---\n$method = new Method('greet');\n$method-\u003esetCodeEval(function ()\n{\n    return 'Hello world!';\n});\n$class-\u003eaddMethod($method);\n// -------------------------------------\n\n// ...\n```\n\nTo add arguments in a specified method, kindy use the following methods below:\n\n| Method               | Description                                          |\n|----------------------|------------------------------------------------------|\n| `addArrayArgument`   | Adds a property with a `array` as its data type.     |\n| `addBooleanArgument` | Adds an argument with a `boolean` as its data type.  |\n| `addClassArgument`   | Adds an argument with the specified class.           |\n| `addFloatArgument`   | Adds an argument with a `float` as its data type.    |\n| `addIntegerArgument` | Adds an argument with an `integer` as its data type. |\n| `addStringArgument`  | Adds an argument with a `string` as its data type.   |\n\n``` php\n// index.php\n\n// ...\n\n$method = new Method('greet');\n$method-\u003eaddBooleanArgument('shout')\n    -\u003ewithDefaultValue(false);\n$method-\u003esetReturn('string');\n$method-\u003esetCodeEval(function ()\n{\n    return 'Hello world!';\n});\n$class-\u003eaddMethod($method);\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n    /**\n     * @param boolean $shout\n     *\n     * @return string\n     */\n    public function greet($shout = false)\n    {\n        return 'Hello world!';\n    }\n}\n```\n\nTo add a class argument without being its type declared, add `withoutTypeDeclared` after `addClassArgument`:\n\n``` php\n// index.php\n\n// ...\n\n$method = new Method('greet');\n$method-\u003eaddClassArgument('test', 'Acme\\Test')\n    -\u003ewithoutTypeDeclared();\n$method-\u003esetReturn('string');\n$method-\u003esetCodeEval(function ($test)\n{\n    return $test-\u003ehello();\n});\n$class-\u003eaddMethod($method);\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n    /**\n     * @param \\Acme\\Test $test\n     *\n     * @return string\n     */\n    public function greet($test)\n    {\n        return $test-\u003ehello();\n    }\n}\n```\n\nA method can also be defined as `protected` or `private`:\n\n``` php\n// index.php\n\n// ...\n\n$method = new Method('greet');\n\n// Set the method as \"protected\" ---\n$method-\u003easProtected();\n// ---------------------------------\n\n// Set the method as \"private\" ---\n$method-\u003easPrivate();\n// -------------------------------\n\n// ...\n```\n\n\u003e [!NOTE]\n\u003e By default, all of the specified methods are in `public` visibility.\n\nThe method can be alternatively be specified as a `@method` tag in the class:\n\n``` php\n// index.php\n\n// ...\n\n$method = new Method('greet');\n\n// ...\n\n// Set as \"@method\" in the class ---\n$method-\u003easTag();\n// ---------------------------------\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @method string greet(boolean $shout = false)\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n}\n```\n\n### Adding properties\n\nSimiliar to adding arguments in a method, adding properties to a class can be done by the following:\n\n| Method               | Description                                         |\n|----------------------|-----------------------------------------------------|\n| `addArrayProperty`   | Adds a property with a `array` as its data type.    |\n| `addBooleanProperty` | Adds a property with a `boolean` as its data type.  |\n| `addClassProperty`   | Adds a property with the specified class.           |\n| `addFloatProperty`   | Adds a property with a `float` as its data type.    |\n| `addIntegerProperty` | Adds a property with an `integer` as its data type. |\n| `addStringProperty`  | Adds a property with a `string` as its data type.   |\n\n``` php\n// index.php\n\n// ...\n\n$class-\u003eaddStringProperty('text')\n    -\u003ewithDefaultValue('Hello world!');\n\n// ...\n\n// Modify \"greet\" method to access \"text\" property ---\n$method-\u003esetCodeEval(function ()\n{\n    return $this-\u003etext;\n});\n// ---------------------------------------------------\n\n// ...\n```\n\n``` php\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n    /**\n     * @var string\n     */\n    protected $text = 'Hello world!';\n\n    /**\n     * @param boolean $shout\n     *\n     * @return string\n     */\n    public function greet($shout = false)\n    {\n        return $this-\u003etext;\n    }\n}\n```\n\nTo change a visibility of a property, the methods `asPublic` and `asPrivate` can be used:\n\n``` php\n// index.php\n\n// ...\n\n$class-\u003eaddStringProperty('text')\n    -\u003easPrivate()\n    -\u003ewithDefaultValue('Hello world!');\n\n// ...\n```\n\n\u003e [!NOTE]\n\u003e By default, all of the specified properties are in `protected` visibility.\n\nAlternatively, the property be specified as a `@property` tag in the class:\n\n``` php\n// index.php\n\n// ...\n\n$class-\u003eaddStringProperty('text')-\u003easTag();\n\n// ...\n```\n\n``` bash\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * @property string $text\n *\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n}\n```\n\n### Setting an empty class\n\nThe `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:\n\n``` php\n// index.php\n\n// ...\n\n$class-\u003esetEmpty();\n\n// ...\n```\n\n``` php\n$ php index.php\n\n\u003c?php\n\nnamespace Acme;\n\nuse Acme\\Hello\\Greeter;\n\n/**\n * Sample class for Acme.\n *\n * @package Acme\n *\n * @author John Doe \u003cjdoe@acme.com\u003e\n */\nclass Greet extends Greeter implements Greetable, Helloable\n{\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG][link-changelog] for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Credits\n\n- [All contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [LICENSE][link-license] for more information.\n\n[ico-build]: https://img.shields.io/github/actions/workflow/status/rougin/classidy/build.yml?style=flat-square\n[ico-coverage]: https://img.shields.io/codecov/c/github/rougin/classidy?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/rougin/classidy.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-version]: https://img.shields.io/packagist/v/rougin/classidy.svg?style=flat-square\n\n[link-build]: https://github.com/rougin/classidy/actions\n[link-changelog]: https://github.com/rougin/classidy/blob/master/CHANGELOG.md\n[link-contributors]: https://github.com/rougin/classidy/contributors\n[link-coverage]: https://app.codecov.io/gh/rougin/classidy\n[link-downloads]: https://packagist.org/packages/rougin/classidy\n[link-license]: https://github.com/rougin/classidy/blob/master/LICENSE.md\n[link-packagist]: https://packagist.org/packages/rougin/classidy","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougin%2Fclassidy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frougin%2Fclassidy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougin%2Fclassidy/lists"}