LoginSignup
2
2

More than 5 years have passed since last update.

PHPでprivate,protectedなstatc変数を外部からアクセス可能にしてセットし直す方法

Last updated at Posted at 2013-11-19

参考:https://bugs.php.net/bug.php?id=54620

ExampleControllerTest.php
public function tearDown() {
    parent::tearDown();
    $_user = (new ReflectionClass('AuthComponent'))->getProperty('_user');
    $_user->setAccessible(1);
    $_user->setValue(array());
}
AuthComponent.php
    protected static $_user = array();

経緯

CakePHPでAuthComponentを利用するControllerのテストを書いていて認証のキャッシュにstatic変数を使っていたので、tearDownのときにセットしなおそうとしたらprotectedだったのでこのようにしました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2