簡體   English   中英

如何測試變量是受保護的還是私有的?

[英]How can I test to see if a variable is protected or private?

我想編寫一個測試來確保變量受到保護。 那可能嗎? 這就是我得到的。

/**
 * @expectedException       Fatal error
 * @expectedExceptionMessage Cannot access protected property
 */

public function testCannotAccessProtectedProperty() {
    $this->assertEquals($this->object->variableiwanttotest[0], $value);
}

這是錯誤消息

PHP Fatal error:  Cannot access protected property Object::variableiwanttotest in /Users/confidential/ObjectTest.php on line 25

這可能是反射的好用法。

http://php.net/manual/en/reflectionclass.getproperties.php

通過使用過濾器,您可以檢索受保護的屬性並檢查其是否存在。

做起來應該很簡單。

$prop = new ReflectionProperty(get_class($object), 'propname'));
$this->assertTrue($prop->isProtected());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM