Другой вызов функции-члена get_segment () по не объектному вопросу - PullRequest
0 голосов
/ 01 июня 2010

Я получаю вышеуказанную ошибку при вызове этого кода:

<?
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 раза, чтобы убить и устранить эту богом забытую ошибку! но ооооооо ....

EDIT: Я изменил базовый класс, добавив __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!-->";
}
}
}
?>

но теперь он говорит: не удается переопределить класс uri в /home/afsadad/public_html/private/funkyphp/funk/funks/libraries/uri.php в строке 3

Ответы [ 3 ]

1 голос
/ 01 июня 2010

При звонке

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

член $this->uri не является объектом; наверное это вообще не определено. Я предполагаю, что метод Core::start() вашего объекта, который определяет $this->uri, не вызывается до Test1::test(). Возможно, вы хотели инициализировать его в конструкторе, __construct()?

0 голосов
/ 31 марта 2012
$CI =& get_instance();
echo $CI->uri->segment(1);
0 голосов
/ 01 июня 2010

Вам требуется файл класса в третьей строке ... используйте require_once () или функцию автозагрузки класса.

...