dongrong9938
2017-03-07 15:11
浏览 85
已采纳

带有类定义的PHP数组声明

in my php code I get this error

Notice: Undefined variable: browserHeader in connectenparse.class.php on line 12

and my code start here with line 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];
    }

My input is

$run = new connectenParse('http://example.com','0');
echo $run->streamParser();

streamParser function takes the variable end returns it. When I create the class with second parameter which is defined for browser header, it must return Mozilla/5.0. Where is the problem?

图片转代码服务由CSDN问答提供 功能建议

在我的php代码中我收到此错误

 注意: 未定义的变量:第12行
   
 
 

中connectenparse.class.php中的browserHeader,我的代码从第9行开始

 <  code> private $ browserHeader = array(“'0'=&gt; Mozilla / 5.0(兼容; Googlebot / 2.1; + http://www.google.com/bot.html)”); 
公共函数connectenParse($  loginPage,$ header =''){
 $ this-&gt; loginPage = $ loginPage; 
 $ this-&gt; browserHeader = $ browserHeader [$ header]; 
} 
    
 
 

我的输入是

  $ run = new connectenParse('http://example.com','0'); 
echo $ run-  &gt; streamParser(); 
   
 
 

streamParser函数接受变量end返回它。 当我使用为浏览器头定义的第二个参数创建类时,它必须返回Mozilla / 5.0。 问题出在哪里?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhangcuo2174 2017-03-07 16:53
    已采纳

    My advise is that you do not need $browserHeader to be an array it is clearly a string and set it as needed.

    private $browserHeader;
    public function connectenParse($loginPage, $header=''){
        $this->loginPage = $loginPage;
        $this->browserHeader = $header;
    }
    

    Then you can call it like

    $run = new connectenParse('http://example.com', "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
    

    If you need the array for multiple other potential headers then you need to fix your array

    const BROWSER_HEADER_LIST = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
    

    Notice it is not ("0=>...") that was making it part of the value. Also it is a const meaning you use it when needed.

    So now you can do something like this

    //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];
        }
    }
    

    Also, connectenParse should probably be __construct($loginPage, $header) class name constructors are deprecated.

    打赏 评论
  • doujing6053 2017-03-07 15:18

    The problem is that you are attempting to access the $header element of the $browserHeader array, without ever defining the $browserHeader array:

    public function connectenParse($loginPage, $header=''){
        $this->loginPage = $loginPage;
        $this->browserHeader = $browserHeader[$header];
    }
    

    On the third line you are assigning a value to $this->browserHeader, which is perfectly fine, but the value you are assigning is $browserHeader[$header]. $header exists, however this method is unaware of any variable called $browserHeader, which is why you're seeing the error. You probable mean to do the following instead:

    $this->browserHeader = $header;

    打赏 评论
  • dsh1956 2017-03-07 15:21

    Your $browerheader variable should be like this

    private $browserHeader = array ("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html");
    

    If you are passing $header value to a function call, you should use

    $this->browserHeader = isset($this->$browserHeader[$header]) ? $this->$browserHeader[$header] : $this->$browserHeader[0];
    

    If $header is a string, you can use it like this

    $this->$browserHeader = $header;
    
    打赏 评论

相关推荐 更多相似问题