Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP值在下一个函数中保持为空_Php_Function_Selenium - Fatal编程技术网

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();在错误的地方,谢谢你指出