PHP访问函数内的函数

Ben*_*Ben 0 php class function

我是PHP类的新手,我只是想知道如何访问PHP类中的函数.

例如:

<?PHP
$cn = "myClass";
$myClass = new $cn;

class myClass
{
    function __construct()
    {
        doSomething(); // ?
    }
    private function doSomething() {
        echo "doSomething accessed!<br />";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

我如何doSomething()在课堂上访问?任何帮助将非常感激.

Mih*_*rga 5

你必须使用$this:

<?PHP
$cn = "myClass";
$myClass = new $cn;

class myClass
{
    function __construct()
    {
        $this->doSomething(); // ?
    }
    private function doSomething() {
        echo "doSomething accessed!<br />";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

当从对象上下文中调用方法时,伪变量$ this可用.$ this是对调用对象的引用.