开发者

php class clarity required please

Very basic i know, but I'm learning :)

So, I have this class:

class RequestPath {
private $parts = array();
    public function __construct() {
        if (isset($_SERVER['PATH_INFO'])) {
            $path = (substr($_SERVER['PATH_INFO'], -1) == "/") ? substr($_SERVER['PATH_INFO'], 0, -1) : $_SERVER['PATH_INFO'];
        } else {
            $path = (substr($_SERVER['REQUEST_URI'], -1) == "/") ? substr($_SERVER['REQUEST_URI'], 0, -1) : $_SERVER['REQUEST_URI'];
        }
            $bits = explode("/", substr($path, 1));
            $parsed['id'] = array_shift($bits); $parsed[] = $parsed['id'];
            $parsed['type'] = array_shift($bits); $parsed[] = $parsed['type'];
            $parts_size = sizeof($bits); if ($parts_size % 2 != 0) {
            $parts_size -= 1;
        }
        for ($i = 0; $i < $parts_size; $i+=2) { $parsed[$bits[$i]] = $bits[$i+1]; $parsed[]开发者_JAVA百科 = $bits[$i+1];
        }
        if (sizeof($bits) % 2 != 0) { $parsed[] = array_pop($bits);
        }
    $this->parts = $parsed;
    }

    public function __get($key) {
        return $this->parts[$key];
    } 
    public function __set($key, $value) {
        $this->_parts[$key] = $value;
    } 
    public function __isset($key) {
        return isset($this->_parts[$key]);
    }
}

I'm having problems returning data from the public functions __get, __set and __isset.

What I would like to do is to run a simple if statement on __isset and then execute some code?

Any ideas?


remove _ in $this->_parts because there is no such var in your code


If i understood your question correctly you want to do something like this ?

$path = new RequestPath();

if(isset($path["foo"])) {
    $x = 1;
} else {
    $x = 2;
}

Also there is a typo in you code of __set and __isset.

You should get an undefined variable warning because you wrote ->_parts instead of ->parts there.

Turn on display_errors and set the error_reporting to include at least E_NOTICE so you don't run into those problems without noticing. (In this case that doesn't help because __get and __set catch that call, but in general it's advice able)


Guys thanks for the tips, indeed the underscore needed to go but the final solution was this:

$path = new RequestPath();

if(isset($path->foo)) {
    $x = 1;
} else {
    $x = 2;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜