cbschuld/Browser.php

Suggestion: Getter/Setter made nicer

IngwiePhoenix opened this issue · 3 comments

Hey there!
When I work with getters and setters, I really like using magic methods since they make things easyer. Here is what i'd recommend for this very, VERY awesome class!

<?php class Browser {

    // ......all the other code


    /*
     * This function will call a getter function to get the information.
     * Example: $Browser->browser;
     * This will actually call $browser->getBrowser(); - it just looks a little more natural.
     * @return mixed
    */  
    public function __get($what) {
        $method = "get".ucfirst($what);
        if(method_exists($this, $method))
            return call_user_func(array($this, $method));
        else
            return $this->$what;
    }

    /*
     * The same as above, just for functions like setUserAgent.
     * @return void
    */
    public function __set($name, $value) {
        $method = "set".ucfirst($name);
        if(method_exists($this, $method))
            return call_user_func(array($this, $method), $value);
        else
            return $this->$name;
    }

    // ......all the other code

}

Just a thought =)

Thanks for the suggestion, we'll look into it. There are a few more changes in the mix so keep an eye out.

You're welcome - and I'll do! ^.^

@IngwiePhoenix - thx; not planning to change the getter/setter structure at least in the 1.9 series; thanks for the feedback. (I realize this is SUPER old; just cleaning up this project a bit finally).