带有类定义的PHP数组声明
在我的php代码中,我得到了这个错误带有类定义的PHP数组声明,php,arrays,declaration,Php,Arrays,Declaration,在我的php代码中,我得到了这个错误 Notice: Undefined variable: browserHeader in connectenparse.class.php on line 12 我的代码从第9行开始 private $browserHeader = array ( "'0' => Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"); public functio
Notice: Undefined variable: browserHeader in connectenparse.class.php on line 12
我的代码从第9行开始
private $browserHeader = array ( "'0' => Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
public function connectenParse($loginPage, $header=''){
$this->loginPage = $loginPage;
$this->browserHeader = $browserHeader[$header];
}
我的意见是
$run = new connectenParse('http://example.com','0');
echo $run->streamParser();
streamParser函数接受变量end并返回它。当我使用为浏览器头定义的第二个参数创建类时,它必须返回Mozilla/5.0。问题出在哪里?问题是您试图访问$browserHeader数组的$header元素,但从未定义过$browserHeader数组: 在第三行,您正在为$this->browserHeader赋值,这很好,但是您正在赋值的值是$browserHeader[$header]$header存在,但是此方法不知道任何名为$browserHeader的变量,这就是您看到错误的原因。您可能打算执行以下操作: $this->browserHeader=$header 您的$browerheader变量应该如下所示
private $browserHeader = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html");
$this->$browserHeader = $header;
//Still have this variable
private $browserHeader;
//use numbers not strings for $header.
public function connectenParse($loginPage, $header=null){
$this->loginPage = $loginPage;
if($header !== null){
$this->browserHeader = self::BROWSER_HEADER_LIST[$header];
}
}
如果要将$header值传递给函数调用,则应使用
$this->browserHeader = isset($this->$browserHeader[$header]) ? $this->$browserHeader[$header] : $this->$browserHeader[0];
如果$header是一个字符串,您可以这样使用它
private $browserHeader = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html");
$this->$browserHeader = $header;
//Still have this variable
private $browserHeader;
//use numbers not strings for $header.
public function connectenParse($loginPage, $header=null){
$this->loginPage = $loginPage;
if($header !== null){
$this->browserHeader = self::BROWSER_HEADER_LIST[$header];
}
}
我的建议是,您不需要$browserHeader作为数组,它显然是一个字符串,并根据需要进行设置
private $browserHeader;
public function connectenParse($loginPage, $header=''){
$this->loginPage = $loginPage;
$this->browserHeader = $header;
}
那么你可以这样称呼它
$run = new connectenParse('http://example.com', "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
如果您需要为多个其他可能的头使用数组,则需要修复数组
const BROWSER_HEADER_LIST = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
请注意,它不是0=>。。。这使它成为价值的一部分。它也是一个const,意思是在需要时使用它
现在你可以做这样的事情了
private $browserHeader = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html");
$this->$browserHeader = $header;
//Still have this variable
private $browserHeader;
//use numbers not strings for $header.
public function connectenParse($loginPage, $header=null){
$this->loginPage = $loginPage;
if($header !== null){
$this->browserHeader = self::BROWSER_HEADER_LIST[$header];
}
}
另外,connectenParse可能应该是uu construct$loginPage,$header类名构造函数已被弃用。是否要$this->browserHeader=$header;?@nerdlyist的可能副本现在返回“0”,而不是浏览器header@u_mulder你是如何看待这个问题和链接主题之间可能存在的分歧的?什么返回0?函数中的$header是字符串还是索引?它返回“0”,我必须使用Mozilla/5.0 header。当我使用为浏览器标题定义的第二个参数创建类时,它必须返回我读过的Mozilla/5.0最佳解决方案。谢谢你,谢谢你。你让我开心: