静态方法和非静态方法的互相调用,类调用非静态方法

静态方法调用非静态方法:
class Test{
    public function fn1(){
        echo 2*2;
//        $this->fn2();
    }

    public static function fn2(){
        echo 3*3;
        $obj = new Test;
        $obj->fn1();
    }
}
//$obj = new Test;
//$obj->fn1();
Test::fn2();
 
  

非静态方法调用静态方法:

class Test{
    public function fn1(){
        echo 2*2;
        $this->fn2();
    }

    public static function fn2(){
        echo 3*3;
//        $obj = new Test;
//        $obj->fn1();
    }
}
$obj = new Test;
$obj->fn1();
//Test::fn2();
可以基本实现,不会报错

类调用非静态方法::


class Facades {
    public function __call($name, $params) {
        return call_user_func_array([$this, $name], $params);
    }
    public static function __callStatic($name, $params) {
        return call_user_func_array([new static(), $name], $params);
    }
}
class Cache extends Facades {
    protected function fn($a, $b) {
        echo "function parameters: ${a} and ${b}
"
; } protected function static_fn($a, $b) { echo "static function parameters: ${a} and ${b}
"
; } }
Cache::fn(3,6);
Cache::static_fn(4,7);

 
  



你可能感兴趣的:(php)