2012-09-13 54 views
0

如何調用屬於該類內同一類的函數?調用類自己的函數錯誤

我:

class BaseConfig { 

public $page ; 
public $htmlRoot = 'assets/html/'; 

public function getPageTitle(){ 
    echo $page = $_GET['p']; 
} 

public function getContent(){ 
    $file = getPageTitle().'.php'; 
    return readfile($htmlRoot.$file); 
} 
} 

我收到以下錯誤,當我打電話

<?PHP Config::getContent();?> 

Fatal error: Call to undefined function getPageTitle() in C:\Xit\xampp\htdocs\IMS3\assets\Config.php on line 17 

我的方式創造我自己的簡單的框架。


Thanks guys,$ this does not work,it just just I can not use out of object context。

'自我'工作非常感謝。

請您詳細說明Radu提到的安全漏洞嗎?


@ S3Mi正在讀取的文件只是html。在這個用例中,我所做的仍然很糟糕,或者可以嗎?

回答

3

您需要的函數名稱前使用$this->

$file = $this->getPageTitle().'.php'; 

如果函數static然後,而不是$this->你會使用self::大部分的時間:

$value = self::someStaticFunction(); 

如果函數static還有一種可能的情況,您可能需要使用late static binding來調用它,例如static::someStaticFunction()。然而,這提示了一個有問題的類設計,所以我只是提到它的完整性。

0
$file = $this->getPageTitle().'.php' ; 
1
class BaseConfig { 

public $page ; 
public $htmlRoot = 'assets/html/'; 

public function getPageTitle(){ 
    return $this->page = $_GET['p']; 
} 

public function getContent(){ 
    $file = $this->getPageTitle().'.php'; 
    return readfile($this->htmlRoot.$file); 
} 
} 

我看到你在不同的文件夾中的文件,我想你沒有在那裏任何關鍵/機密數據,但它並沒有解決接入到其它文件夾。

可以將$ _GET ['p']設置爲'../index.php'並獲取您的php代碼。這是一個很大的安全問題。

我建議你閱讀關於輸入消毒和驗證。

永遠不會通過readfile()或任何傳遞原始內容的函數來提供.php文件。 .php文件應該由PHP解釋。暴露.php代碼非常糟糕。