2010-05-31 89 views
0

調用此代碼時,我得到上述錯誤:另一個調用一個成員函數get_segment()上的非對象問題

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

我得到其中commentated錯誤。

這個類繼承這個類:

<?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和Delimb這個godforsaken錯誤!但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!-->"; 
} 
} 
} 
?> 

但現在它說:不能重新聲明類URI在/ home/afsadad /的public_html /私營/ funkyphp /funk/funks/libraries/uri.php上線3

回答

0

您需要第三行的類文件...使用require_once()或類自動加載函數。

1

當調用

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

的構件$this->uri不是一個對象;可能它根本沒有定義。我想你的對象的Core::start()定義$this->uri的方法不會在Test1::test()之前調用。也許你想在構造函數中初始化它,__construct()

+0

我用__construct編輯了OP,但現在它帶有一個新的錯誤。請參閱編輯的OP。 – user354866 2010-05-31 21:31:44

+0

好吧,你們兩個都幫了我,可惜我不能把你們倆都當成正確的或者是積極的 – user354866 2010-05-31 21:46:41

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

不會這樣做,建立一個不贊成的通知嗎? – Peon 2012-10-29 08:36:58

相關問題