Reputation:
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?
Upvotes: 0
Views: 52
Reputation: 1691
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;
Upvotes: 0
Reputation: 2847
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.
Upvotes: 0
Reputation: 160
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;
Upvotes: 1