开发者

Another Call to a member function get_segment() on a non-object question

I get the above error when calling this code:

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

I get the error where commentated.

This class extends this class:

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

And here is the contents of 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;
    }
}
?>

i have asked a similar question to this before but the answer does not apply here.

I have rewritten my code 3 times to KILL and Delimb this godforsaken error! but nooooooo....

EDIT: I have changed the core class to add __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!-->";
}
}
}
?>

but now it says: Cannot redeclare class uri in /home/afsadad/public_html/private/funkyphp/funk/funks/libraries/uri.php on line 3


When calling

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

the member $this->uri is not an object; probably it is not defined at all. I guess that your object's Core::start() method that defines $this->uri is not called prior to Test1::test(). Perhaps you wanted to initialize it in the constructor, __construct()?


You are requiring a classfile on line three... use require_once(), or a class autoloading function.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜