静态方法与非静态方法 [英] static method vs non-static method

查看:101
本文介绍了静态方法与非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是静态方法和非静态方法的php类代码示例.

Below are the examples of php class code that is static method and non static method.

示例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.

示例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.

我试图弄清楚这两个类之间的区别是什么.

I am trying to figure out what is the difference between these two Classes.

正如我们在非静态方法的结果中看到的那样,定义了"$ this".

As we can see on the result of the none static method, the "$this" was defined.

但是另一方面,即使静态方法都被实例化,也没有定义静态方法的结果.

But on the other hand the result on the static method was not defined even they were both instantiated.

我想知道为什么自从它们都被实例化后它们会有不同的结果?

I am wondering why they have different result since they were both instantiated?

请您告诉我这些代码上正在发生的事情.

Could you please enlighten me on what is happening on these codes.

推荐答案

在进行下一步之前,请进入始终的习惯,指定对象的属性和方法的可见性/可访问性.不用写

Before we go any further: Please, get into the habbit of always specifying the visibility/accessibility of your object's properties and methods. Instead of writing

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,而方法仅仅是具有指向该结构的指针的函数.至少,这就是要旨,更多详细信息在这里

first off, your first example A::foo will trigger a notice (calling a non-static method statically always does this).
Secondly, in the second example, when calling A::foo(), PHP doesn't create an on-the-fly instance, nor does it call the method in the context of an instance when you called $a->foo() (which will also issue a notice BTW). Statics are, essentially, global functions because, internally, PHP objects are nothing more than a C struct, and methods are just functions that have a pointer to that struct. At least, that's the gist of it, more details here

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

The main difference (and if used properly benefit) of a static property or method is, that they're shared over all instances and accessible globaly:

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是公开的,则您甚至都不需要实例来更改各处的属性:

As you can see, the static property $bar can't be set on each instance, if its value is changed, the change applies for all instances.
If $bar were public, you wouldn't even need an instance to change the property everywhere:

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?'

查看工厂模式,并(纯粹提供信息)也查看Singleton模式.就Singleton模式而言:还用Google为何使用它. IoC,DI,SOLID是您很快就会遇到的缩写.了解它们的含义,并弄清楚为什么他们(以自己的方式)选择Singletons

Look into the factory pattern, and (purely informative) also peek at the Singleton pattern. As far as the Singleton pattern goes: Also google why not to use it. IoC, DI, SOLID are acronyms you'll soon encounter. Read about what they mean and figure out why they're (each in their own way) prime reasons to not go for Singletons

这篇关于静态方法与非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆