2011-09-10 119 views
0

我有這個代碼的第36行的問題。幫助與瀏覽器檢測代碼

<?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 ; 
    } 

} 

?> 

這是有錯誤die "No such property or function $name" ;任何幫助,將撥出的線。

+1

我用:get_browser @ HTTP: //php.net/manual/en/function.get-browser.php –

+1

我很好奇,錯誤是什麼? – ajreal

+0

當沒有找到類的屬性並且沒有拋出異常時,你真的希望PHP腳本死掉嗎?爲什麼不創建公共屬性'Name'和'Version'? – CodeCaster

回答

2

線需要是這樣,而是:

die("No such property or function $name"); 

更多信息,請參見here

+0

它確實接受只是'死; – ajreal

1

你爲什麼不重寫代碼來此?:

<?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 ; 
      } 
     } 
    } 
} 

?> 

不管怎麼說,我建議你使用一些已經存在的庫來檢測瀏覽器,如http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/