Php ReflectionProperty构造函数
如果有人能解释我的以下行为,我将不胜感激(同时也更加明智;): 我们有课Php ReflectionProperty构造函数,php,reflection,Php,Reflection,如果有人能解释我的以下行为,我将不胜感激(同时也更加明智;): 我们有课 class Test { public $id; private $name; protected $color; } 还有ReflectionProperty构造函数的行为,我还不完全理解 第一工作组: function check() { $class = new Test(); $ref = new ReflectionObject($class); $pros =
class Test {
public $id;
private $name;
protected $color;
}
还有ReflectionProperty构造函数的行为,我还不完全理解
第一工作组:
function check() {
$class = new Test();
$ref = new ReflectionObject($class);
$pros = $ref->getProperties();
foreach ($pros as $pro) {
false && $pro = new ReflectionProperty();
print_r($pro);
}
}
这将提供以下各项的正确输出:
ReflectionProperty Object
(
[name] => id
[class] => Test
)
ReflectionProperty Object
(
[name] => name
[class] => Test
)
ReflectionProperty Object
(
[name] => color
[class] => Test
)
现在:如果我从此行中删除“false”:
false && $pro = new ReflectionProperty();
输出将是:
PHP Fatal error: Uncaught ArgumentCountError: ReflectionProperty::__construct() expects exactly 2 parameters, 0 given
ReflectionProperty::_构造()采用($class,$name)
因此,问题是:
为什么“false”一开始就起作用?
false&&$pro=new ReflectionProperty()代码>计算结果为false
由于第一个条件为“假”,因此无需对第二个条件进行求值$pro=new ReflectionProperty()
(称为“短路求值”)
当您删除false
时,您有一行
$pro = new ReflectionProperty();
而ReflectionProperty
constructor需要两个参数(错误消息会告诉您这一点)。false&&$pro=new ReflectionProperty()代码>计算结果为false
由于第一个条件为“假”,因此无需对第二个条件进行求值$pro=new ReflectionProperty()
(称为“短路求值”)
当您删除false
时,您有一行
$pro = new ReflectionProperty();
而ReflectionProperty
constructor需要两个参数(错误消息会告诉您这一点)。这与ReflectionProperty构造函数本身无关
false&&$pro=newReflectionProperty();
是一种叫做短路的东西。这意味着右边的代码只有在需要时才会执行。在本例中,由于您在左侧为false的情况下执行&(AND),因此引擎知道结果永远不可能等于true,因此它不需要执行和计算右侧,这是您的ReflectionProperty构造函数
基本上,false&&将停止运行中断的代码,然后print\r将使用getProperties结果中现有的pro值 这与ReflectionProperty构造函数本身无关
false&&$pro=newReflectionProperty();
是一种叫做短路的东西。这意味着右边的代码只有在需要时才会执行。在本例中,由于您在左侧为false的情况下执行&(AND),因此引擎知道结果永远不可能等于true,因此它不需要执行和计算右侧,这是您的ReflectionProperty构造函数
基本上,false&&将停止运行中断的代码,然后print\r将使用getProperties结果中现有的pro值 对于$a&&$b,$b仅当为真时进行评估$a条件对于$a&&$b,$b仅当为真时进行评估$a条件两个答案都完美地解释了主题;希望你们不介意我接受新的答案。两个答案都完美地解释了主题;希望你不介意我接受新的答案。