Php 静态方法与非静态方法

Php 静态方法与非静态方法,php,class,oop,methods,static,Php,Class,Oop,Methods,Static,下面是静态方法和非静态方法的php类代码示例 例1: class A{ //None Static method function foo(){ if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")<br>"; } else { echo "

下面是静态方法和非静态方法的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.
A类{
//非静态法
函数foo(){
如果(isset($this)){
回显“$这是定义的(”;
echo get_类($this);
回声“
”; }否则{ echo“\$this未定义。
”; } } } $a=新的a(); $a->foo(); A::foo(); //结果 $this定义为(A) $this未定义。
例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";
        }
    }
 }

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

 //result
 $this is not defined.
 $this is not defined.
A类{
//静态法
静态函数foo(){
如果(isset($this)){
回显“$这是定义的(”;
echo get_类($this);
回声“
\n”; }否则{ echo“\$this未定义。
\n”; } } } $a=新的a(); $a->foo(); A::foo(); //结果 $this未定义。 $this未定义。
我试图找出这两个类之间的区别

正如我们在none-static方法的结果中所看到的,定义了“$this”

但另一方面,静态方法的结果没有定义,即使它们都被实例化了

我想知道,既然它们都是实例化的,为什么它们会有不同的结果


在我们进一步讨论之前,请您告诉我这些代码中发生了什么事情。

请进入始终指定对象属性和方法的可见性/可访问性的习惯。而不是写作

function foo()
{//php 4 style method
}
写:

public function foo()
{
    //this'll be public
}
protected function bar()
{
    //protected, if this class is extended, I'm free to use this method
}
private function foobar()
{
    //only for inner workings of this object
}
首先,您的第一个示例
A::foo
将触发一个通知(静态调用非静态方法总是这样做)
其次,在第二个示例中,当调用
A::foo()
时,PHP不会创建动态实例,也不会在调用
$A->foo()
时在实例上下文中调用该方法(顺便说一句,这也会发出通知)。静态本质上是全局函数,因为在内部,PHP对象只不过是一个C
struct
,而方法只是具有指向该结构的指针的函数。至少,这是它的要点

静态属性或方法的主要区别(以及如果使用得当的好处)是,它们在所有实例上共享,并且可以全局访问:

class Foo
{
    private static $bar = null;
    public function __construct($val = 1)
    {
        self::$bar = $val;
    }
    public function getBar()
    {
        return self::$bar;
    }
}
$foo = new Foo(123);
$foo->getBar();//returns 123
$bar = new Foo('new value for static');
$foo->getBar();//returns 'new value for static'
如您所见,不能在每个实例上设置静态属性
$bar
,如果其值更改,则更改适用于所有实例。
如果
$bar
是公共的,您甚至不需要一个实例来更改任何地方的属性:

class Bar
{
    public $nonStatic = null;
    public static $bar = null;
    public function __construct($val = 1)
    {
        $this->nonStatic = $val;
    }
}
$foo = new Bar(123);
$bar = new Bar('foo');
echo $foo->nonStatic, ' != ', $bar->nonStatic;//echoes "123 != foo"
Bar::$bar = 'And the static?';
echo $foo::$bar,' === ', $bar::$bar;// echoes 'And the static? === And the static?'

查看工厂模式,并且(纯粹是信息性的)也可以查看单例模式。就单例模式而言:谷歌为什么不使用它呢。IoC、DI、SOLID是您很快就会遇到的首字母缩略词。阅读它们的含义,找出它们(各自以自己的方式)不使用单例的主要原因。有时,您需要使用一个方法,但不需要调用类,因为类中的某些函数会自动调用,如“构造”、“析构函数”,。。。(神奇的方法)

调用类:

$a = new A();
$a->foo();
不调用类:(只运行foo()函数)


因为静态方法无需实例化即可调用。所以$this不可用现在我理解为什么结果没有在静态方法上定义,而不是在静态方法上使用$this应该使用“self”。我说的对吗?如果你想调用一个函数,但不想调用构造函数/析构函数,不要将该函数声明为方法。。。
A::foo();