未捕获错误:类名必须是有效对象或字符串

Dar*_*nja -1 php oop

我只是试图获得该功能,something但我只是不断收到错误

未捕获错误:类名必须是有效对象或字符串

任何的想法?

class A {
    private $a;
    private $b;
    function __construct($a,$b){
        $this->a = ( $a == NULL) ? ' something' : $a;
        $this->b = ( $b == NULL) ? ' something' : $b;
        echo($this->a);
    }
    private function geta(){
        return $this->a;
    }
    private function getb(){
        return $this->b;
    }
    public static function something(){
        echo $this->a;
        echo $this->b;
    }
}
$o = new A('hi','something');
$o->A::something();
Run Code Online (Sandbox Code Playgroud)

Ale*_*eri 6

请试试这个:

$o::something();
Run Code Online (Sandbox Code Playgroud)

您不需要引用A因为$o已经class A可以直接调用静态函数something()

但你在$this里面使用static function这是不可能的!

所以你需要删除static函数的类型,因为在函数内部你不能使用$this 所以你需要这样做:

public function something(){
    echo $this->a;
    echo $this->b;
}
Run Code Online (Sandbox Code Playgroud)

并以这种方式调用它:

$o = new A('hi','something');
$o->something();
Run Code Online (Sandbox Code Playgroud)