stefna/php-code-builder
最新稳定版本:2.5.0
Composer 安装命令:
composer require stefna/php-code-builder
包简介
README 文档
README
Installation
$ composer require stefna/php-code-builder
API
PhpFile
The actual file to save.
Methods
setStrict()
Mark files with declare(strict_types=1);
setNamespace(string $ns)
Set file namespace
setSource(string $code)
Set raw php code to be included in file
Like
$file = new PhpFile(); $file->setSource("spl_autoload_register('$autoloaderName');");
addFunction(PhpFunction $func)
Add function to file. Files can contain multiple functions
addClass(PhpClass $class)
Add class to file. Files can contain multiple classes
addTrait(PhpTrait $trait)
Add trait to file. Files can contain multiple traits
addInterface(PhpInterface $interface)
Add interface to file. Files can contain multiple interfaces
PhpParam
Used to make complex parameter arguments
Usage
new PhpParam('string', 'test') => string $test new PhpParam('object', 'test', null) => object $test = null $param = new PhpParam('DateTimeInterface', 'date'); $param->allowNull(true); $param->getSource() => ?DateTimeInterface $date $param = new PhpParam('float', 'price', 1.5); $param->allowNull(true); $param->getSource() => ?float $price = 1.5
PhpFunction
Usage
__construct(string $identifier, array $params, string $source, ?PhpDocComment $comment = null, ?string $returnTypeHint = null)
Example
$func = new PhpFunction( 'testFunc', [ 'param1', // Simple parameter new PhpParam('int', 'param2', 1), ], "return $param1 + $param2", null, // no docblock 'int' ); echo $func->getSource();
Output:
function testFunc($param1, int $param2 = 1): int { return $param1 + $param2; }
PhpMethod
Method is an extension of PhpFunction with support for accessors like public, protected, private, final, static and abstract
Usage
With return typehint
$method = new PhpMethod('private', 'test', [], 'return 1', null, 'int'); echo $method->getSource(); ------- private function test(): int { return 1; }
With docblock
$method = new PhpMethod( 'private', 'test', [], 'return 1', new PhpDocComment('Test Description') ); echo $method->getSource(); ------- /** * Test Description */ private function test() { return 1; }
Static method
$method = new PhpMethod('protected', 'test', [], 'return 1'); $method->setStatic(); echo $method->getSource(); ------- protected static function test() { return 1; }
Final method
$method = new PhpMethod('public', 'test', [], 'return 1'); $method->setFinal(); echo $method->getSource(); ------- final public function test() { return 1; }
Abstract method
$method = new PhpMethod('public', 'test', [], 'return 1', null, 'int'); $method->setAbstract(); echo $method->getSource(); ------- abstract public function test(): int;
统计信息
- 总下载量: 9.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-07