另一个成员函数get_segment()调用非对象问题

时间:2010-05-31 20:58:09

标签: php object member

调用此代码时出现上述错误:

<?
class Test1 extends Core {
function home(){
?>
This is the INDEX of test1
<?
}
function test2(){
echo $this->uri->get_segment(1); //this is where the error comes from
?>
This is the test2 of test1 testing URI
<?
}
}
?>

我收到评论的错误。

这个类扩展了这个类:

<?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/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

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

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

以下是uri.php的内容:

<?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;
    }
}
?>
我以前曾向此提出过类似的问题,但答案不适用于此。

我已经将我的代码重写了3次以杀死KILL并将这个荒谬的错误传达给我!但是nooooooo ....

编辑: 我已经更改了核心类以添加__construct:

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

public function start()
{

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

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

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

但现在它说:不能在第3行的/home/afsadad/public_html/private/funkyphp/funk/funks/libraries/uri.php重新声明类uri

3 个答案:

答案 0 :(得分:1)

致电

$this->uri->get_segment(1);

成员$this->uri不是对象;可能它根本没有定义。我猜您定义Core::start()的对象的$this->uri方法不会在Test1::test()之前调用。也许你想在构造函数__construct()

中初始化它

答案 1 :(得分:0)

您需要第三行的类文件...使用require_once()或类自动加载功能。

答案 2 :(得分:0)

$CI =& get_instance();
echo $CI->uri->segment(1);