调用类自身的函数错误


Calling classes own function error

如何调用属于该类中同一类的函数?

我有:

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

顺便说一下,我正在创建自己的简单框架。


谢谢大家,$this不起作用,它只是说我不能在对象上下文之外使用它。

"self"很管用,谢谢。

你能详细说明拉杜提到的安全漏洞吗?


@S3Mi正在读取的文件只是html。在这个用例中,我所做的仍然是糟糕的还是可以的?

您需要在函数名之前使用$this->

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

如果函数是static,那么大部分时间都会使用self::而不是$this->

$value = self::someStaticFunction();

如果函数是static,那么在可能的情况下,您可能需要使用后期静态绑定来调用它,例如static::someStaticFunction()。然而,这暗示了一个有问题的类设计,所以我只是为了完整性而提到它。

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代码是非常糟糕的。

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