php-extended/php-inspect

此包已被弃用且不再维护。作者建议使用 php-extended/php-inspector-object 包。

一个库,用于安全地告诉变量是什么

2.0.1 2020-02-16 10:20 UTC

This package is auto-updated.

Last update: 2020-08-17 19:16:03 UTC


README

一个库,用于安全地告诉变量是什么。

安装

此库的安装是通过 composer 进行的。从 他们的网站 下载 composer.phar。然后将其添加到您的 composer.json 中

	"require": {
		...
		"php-extended/php-inspect": "^2",
		...
	}

然后运行 php composer.phar update 以安装此库。此库的所有类的自动加载是通过 composer 的自动加载器完成的。

基本用法

在您的代码中(例如,当尝试在异常中使用变量时)


use PhpExtended\Inspect\Inspector;

if(!'<condition not satisfied with $myVar >')
{
	throw new Exception(strtr(
		'The given variable "myVar" is not an object, it\'s a {thing}.',
		['{thing}' => Inspector::inspect($myVar)]
	));
}

预期的结果是以下内容


use PhpExtended\Inspect\Inspector;

echo Inspector::inspect(null); 					// echo 'null'
echo Inspector::inspect(false); 				// echo 'boolean'
echo Inspector::inspect('myvar');				// echo 'string'
echo Inspector::inspect(1);						// echo 'integer'
echo Inspector::inspect(1.5);					// echo 'float'
echo Inspector::inspect(new stdClass()); 		// echo 'object(stdClass)'
echo Inspector::inspect(fopen(__FILE__, 'r'));	// echo 'resource(stream)'
echo Inspector::inspect([1]); 					// echo 'array(integer)'
echo Inspector::inspect([1.5]);					// echo 'array(float)'
echo Inspector::inspect([1, 1.5]);				// echo 'array(integer,float)'
echo Inspector::inspect([1, 2, 3]);			// echo 'array(integer)'
echo Inspector::inspect([new stdClass()]);	// echo 'array(object(stdClass))'
echo Inspector::inspect([[1]]);					// echo 'array(array(integer))'

许可证

MIT (见 许可证文件).