Php 浏览器检测代码帮助
我对这段代码的第36行有问题Php 浏览器检测代码帮助,php,Php,我对这段代码的第36行有问题 <?PHP class Browser { private $props = array("Version" => "0.0.0", "Name" => "unknown", "Agent" => "unknown") ; public function __Construct() {
<?PHP
class Browser
{
private $props = array("Version" => "0.0.0",
"Name" => "unknown",
"Agent" => "unknown") ;
public function __Construct()
{
$browsers = array("firefox", "msie", "opera", "chrome", "safari",
"mozilla", "seamonkey", "konqueror", "netscape",
"gecko", "navigator", "mosaic", "lynx", "amaya",
"omniweb", "avant", "camino", "flock", "aol");
$this->Agent = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach($browsers as $browser)
{
if (preg_match("#($browser)[/ ]?([0-9.]*)#", $this->Agent, $match))
{
$this->Name = $match[1] ;
$this->Version = $match[2] ;
break ;
}
}
}
public function __Get($name)
{
if (!array_key_exists($name, $this->props))
{
die "No such property or function $name" ;
}
return $this->props[$name] ;
}
public function __Set($name, $val)
{
if (!array_key_exists($name, $this->props))
{
SimpleError("No such property or function.", "Failed to set $name", $this->props) ;
die ;
}
$this->props[$name] = $val ;
}
}
?>
这是一行,错误是“没有这样的属性或函数$name”代码>任何帮助都将被占用。行应该是这样,而不是:
die("No such property or function $name");
有关更多信息,请参阅。行必须是以下内容:
die("No such property or function $name");
有关更多信息,请参阅。为什么不将该代码重写为此?:
<?php
class Browser
{
public $Version = "0.0.0";
public $Name = "unknown";
public $Agent = "unknown";
public function __Construct()
{
$browsers = array("firefox", "msie", "opera", "chrome", "safari",
"mozilla", "seamonkey", "konqueror", "netscape",
"gecko", "navigator", "mosaic", "lynx", "amaya",
"omniweb", "avant", "camino", "flock", "aol");
$this->Agent = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach($browsers as $browser)
{
if (preg_match("#($browser)[/ ]?([0-9.]*)#", $this->Agent, $match))
{
$this->Name = $match[1] ;
$this->Version = $match[2] ;
break ;
}
}
}
}
?>
不管怎样,我建议您使用一些已经存在的库来检测浏览器,比如,为什么不将代码重写成这样呢
<?php
class Browser
{
public $Version = "0.0.0";
public $Name = "unknown";
public $Agent = "unknown";
public function __Construct()
{
$browsers = array("firefox", "msie", "opera", "chrome", "safari",
"mozilla", "seamonkey", "konqueror", "netscape",
"gecko", "navigator", "mosaic", "lynx", "amaya",
"omniweb", "avant", "camino", "flock", "aol");
$this->Agent = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach($browsers as $browser)
{
if (preg_match("#($browser)[/ ]?([0-9.]*)#", $this->Agent, $match))
{
$this->Name = $match[1] ;
$this->Version = $match[2] ;
break ;
}
}
}
}
?>
不管怎样,我建议您使用一些已经存在的库来检测浏览器,比如I use:get_browser@I m curios,错误是什么?您真的希望PHP脚本在未找到类的属性且未引发异常时死亡()?为什么不创建公共属性
Name
和Version
?您真的想在PHP中添加浏览器检测吗?这是非常复杂的,而且永远不会完全正确。有很多库都尝试这样做,但没有一个是完美的。您最好将浏览器检测,或者更确切地说:功能检测留给客户端代码。他们只是在另一个问题中向我建议了这一点。我使用:get_browser@I'm curios,错误是什么?当找不到类的属性并且不引发异常时,您真的希望PHP脚本死亡()吗?为什么不创建公共属性Name
和Version
?您真的想在PHP中添加浏览器检测吗?这是非常复杂的,而且永远不会完全正确。有很多库都尝试这样做,但没有一个是完美的。您最好将浏览器检测,或者更确切地说:功能检测留给客户端代码。他们只是在另一个问题中建议我这样做。。