PHP值在下一个函数中保持为空
我试图修复一个测试,但不知何故,在下一个函数中,我的值一直是PHP值在下一个函数中保持为空,php,function,selenium,Php,Function,Selenium,我试图修复一个测试,但不知何故,在下一个函数中,我的值一直是NULL。 我正在使用Sebastian Bergmann提供的Selenium webdriver 所以我有: protected $role; // Setup .... public function testRoles() { // Code .... $this->role = 'hr'; // I want this value in the next function } public function
NULL
。
我正在使用Sebastian Bergmann提供的Selenium webdriver
所以我有:
protected $role;
// Setup ....
public function testRoles()
{
// Code ....
$this->role = 'hr'; // I want this value in the next function
}
public function testAcl()
{
// Login here
var_dump($this->role); // This var_dump is NULL , why?
$this->webDriver->getKeyboard()->sendKeys($this->role);
}
当我var\u dump
$this->role
时,它给出NULL
问题:这是为什么?我忘记了什么/我能做什么使它成为我想要的数据:“hr”
我收到的错误:为foreach()提供的参数无效。
这是因为
$this->role
为NULL
这是因为您从未在var\u dump($this->role)之前设置角色;线路。这意味着您必须在var_dump($this->role);)之前运行$this->testRoles() 调用testRoles()设置$this roles
public function testAcl()
{
// call testRoles
$this->testRoles();
// now $this->role was set to hr by testRoles()
// work with it
var_dump($this->role);
}
您从未在testAcl()之前调用过testRoles()?您是否在同一页面周期中调用
testRoles()
和testAcl()
,顺序正确?虽然它以前调用过,但我目前找到了一个解决方法并创建了一个私有函数,然后请将您的解决方法作为答案发布,因为这似乎非常有用odd@user574632 ,jens andré的回答在我的解决方法之前解决了这个问题。好吧,我知道我做错了什么,我在做$this->testRoles()代码>在错误的地方,谢谢你指出它好吧,我知道我做错了什么,我在做$this->testRoles();在错误的地方,谢谢你指出