小编oci*_*ugi的帖子

静态方法与非静态方法

下面是php类代码的例子,静态方法和非静态方法。

示例 1:

class A{
    //None Static method
    function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>";
        } else {
            echo "\$this is not defined.<br>";
        }
    }
 }

 $a = new A();
 $a->foo();
 A::foo();

 //result
 $this is defined (A)
 $this is not defined.
Run Code Online (Sandbox Code Playgroud)

示例 2:

class A{
    //Static Method
    static function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>\n";
        } else {
            echo "\$this is not defined.<br>\n";
        }
    }
 } …
Run Code Online (Sandbox Code Playgroud)

php oop methods static class

3
推荐指数
1
解决办法
6910
查看次数

标签 统计

class ×1

methods ×1

oop ×1

php ×1

static ×1