调用成员函数get_segment()错误

时间:2010-05-31 20:14:04

标签: php object

我在使用这段PHP代码时遇到了这个问题:

class Core {
public function start()
{
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$uri->get_segment(1).".php")){

只有一段代码

我能解释它的最好方法是它是一个调用另一个类(uri.php)的类,我收到错误:致命错误:在/中的非对象上调用成员函数get_segment()第11行的home / eeeee /public_html/private/funkyphp/funk/core/core.php(if($ this-> uri-> get_segment(1)部分)

我遇到了很多这个问题,这真的让我烦恼。

库代码是:

<?php
class uri
{
    private $server_path_info = '';
    private $segment = array();
    private $segments = 0;

    public function __construct()
    {
        $segment_temp = array();
        $this->server_path_info = preg_replace("/\?/", "", $_SERVER["PATH_INFO"]);
        $segment_temp = explode("/", $this->server_path_info);
        foreach ($segment_temp as $key => $seg)
        {
            if (!preg_match("/([a-zA-Z0-9\.\_\-]+)/", $seg) || empty($seg)) unset($segment_temp[$key]);
        }
        foreach ($segment_temp as $k => $value)
        {
            $this->segment[] = $value;
        }
        unset($segment_temp);
        $this->segments = count($this->segment);
    }

    public function segment_exists($id = 0)
    {
        $id = (int)$id;
        if (isset($this->segment[$id])) return true;
        else return false;
    }

    public function get_segment($id = 0)
    {
    $id--;
        $id = (int)$id;
        if ($this->segment_exists($id) === true) return $this->segment[$id];
        else return false;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您对get_segment()的来电不一致。

在一个案例中,您拨打$this->uri->get_segment(),根据您之前的代码,这是正确的。第二次调用$uri->get_segment时,$this->缺少{{1}},因此不是有效对象。

相关问题