PHPackages                             popphp/pop-code - 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. [Framework](/categories/framework)
4. /
5. popphp/pop-code

ActiveLibrary[Framework](/categories/framework)

popphp/pop-code
===============

Pop Code Component for Pop PHP Framework

5.0.5(6mo ago)57.0k↓50%2BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Jul 16Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-code)[ Packagist](https://packagist.org/packages/popphp/pop-code)[ Docs](https://github.com/popphp/pop-code)[ RSS](/packages/popphp-pop-code/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (26)Used By (2)

pop-code
========

[](#pop-code)

[![Build Status](https://github.com/popphp/pop-code/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-code/actions)[![Coverage Status](https://camo.githubusercontent.com/21c4d38ac5f504f8da5e5043ed3b813839195b983cf5608324d7f1773d4ca17f/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d636f6465)](http://cc.popphp.org/pop-code/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Generate Code](#generate-code)
- [Parse Code](#parse-code)

Overview
--------

[](#overview)

`pop-code` provides the ability to dynamically generate PHP code on the fly as well as parse and modify existing PHP code.

`pop-code` is a component of the [Pop PHP Framework](https://www.popphp.org/).

[Top](#pop-code)

Install
-------

[](#install)

Install `pop-code` using Composer.

```
composer require popphp/pop-code

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-view" : "^5.0.5"
}

```

[Top](#pop-code)

Quickstart
----------

[](#quickstart)

### Create a simple function

[](#create-a-simple-function)

In this example, a function is created and rendered to a string:

```
use Pop\Code\Generator;

$function = new Generator\FunctionGenerator('sayHello');
$function->addArgument('name', 'null', 'string');
$function->setBody("echo 'Hello ' . \$name;");
$function->addReturnType('void');
$function->setDesc('This is the first function');

echo $function;
```

```
/**
 * This is the first function
 *
 * @param  string|null  $name
 * @return void
 */
function sayHello(string|null $name = null): void
{
    echo 'Hello ' . $name;
}
```

### Create a simple class

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

In this example, a class is created and saved to a file:

```
use Pop\Code\Generator;

// Create the class object and give it a namespace
$class = new Generator\ClassGenerator('MyClass');
$class->setNamespace(new Generator\NamespaceGenerator('MyApp'));

// Create a new protected property with a default value
$prop = new Generator\PropertyGenerator('foo', 'string', null, 'protected');

// Create a method and give it an argument, body and docblock description
$method = new Generator\MethodGenerator('setFoo', 'public');
$method->addArgument('foo', null, 'string')
    ->setBody('$this->foo = $foo;')
    ->addReturnType('void')
    ->setDesc('This is the method to set foo.');

// Add the property and the method to the class code object
$class->addProperty($prop);
$class->addMethod($method);

// Save the class to a file
$code = new Generator($class);
$code->writeToFile('MuClass.php');
```

The contents of the file will be:

```
