adamquaile/php-global-abstraction

封装影响PHP全局作用域的特性的包装器

v2.0.0 2015-02-04 16:57 UTC

README

此库提供了一些PHP影响全局作用域的特性的面向对象包装器。

当前实现的特性有

  • 常量
  • 函数
  • Echo / Print

全局状态是邪恶的。尽可能避免它。不要认为这个库使它变得可以接受。

此库有两个主要用例

  • 当你在一个不可避免的环境中工作时,你仍然希望你的代码尽可能可测试
  • 当你正在重构代码以摆脱这个混乱

使用方法

使用composer安装它,adamquaile/php-global-abstraction

常量

<?php

$constants = new \AdamQuaile\PhpGlobal\Constants\ConstantWrapper();

$constants->set('key', 'value');
$constants->get('key');
$constants->isDefined('key');

函数

<?php

$functions = new \AdamQuaile\PhpGlobal\Functions\FunctionWrapper(
    new FunctionCreator(),
    new FunctionInvoker()
);

# Create function with a specified name

$functions->create($callable, 'func_in_global_scope');
\func_in_global_scope($arguments);


# Create function and return its automatically generated name

$functionName = $functions->create($callable);
$$functionName($arguments);

# Call a function existing in global scope
$functions->invoke('strlen', 'hello world');

Echo / Print

<?php

$output = new \AdamQuaile\PhpGlobal\Output\EchoWrapper();

$output->output('Hello ', $world);