0

I have a problem with the line 36 of this code.

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

}

?> 

this is the line that have the error die "No such property or function $name" ; any help would be appropriated.

5
  • 1
    I use: get_browser @ php.net/manual/en/function.get-browser.php Commented Sep 10, 2011 at 15:09
  • 1
    I'm curios, what is the error? Commented Sep 10, 2011 at 15:13
  • Do you really want the PHP script to die() when a property of a class is not found and not throw an exception? And why don't you just create public properties Name and Version? Commented Sep 10, 2011 at 15:16
  • 2
    Do you really want to add browser detection to PHP? It is Very Complex and never completely right. There are many libraries that try to do this, but no one is perfect. You better leave browser detection, or rather: functionality detection to the client side code. Commented Sep 10, 2011 at 15:20
  • They just suggested me this chrisschuld.com/projects/… in another question.. Commented Sep 10, 2011 at 15:23

2 Answers 2

2

The line needs to be this, instead:

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

See here for more info.

Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you rewrite that code to this?:

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

?>

Anyways, I suggest you using some already-existing library to detect browser, like http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.