PHP-静态与实例方法 [英] PHP - Static vs Instance Method

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

问题描述

我有点困惑,因为我没有在PHP中进行OOP的丰富经验.我总是听到使用实例方法比使用静态方法更好.为什么?

I'm a little bit confused as i do not have much experience in OOP in PHP. I always hear that using instance methods is better practice than using static methods. Why?

我需要一个深入的回答并附上解释.

I need a deep answer with an explanation please.

例如,为什么下面要用实例方法来完成?

For example why this below should be done with instance method?

控制器:

public function getProperty($id){
        $property = Property::getProperty($id);
        return $property;
}

型号:

public static function getProperty($id){
        //$query = DB::table('properties')...
        //Some Code;
        return $query;         
}

我正在阅读有关setter/getter,实例vs静态等的信息,但是我需要一个完整的答案来理解事物的操作方式为什么.

I'm reading about setter/getter, instance vs static and etc. But I need a complete answer to understand the how and why of things.

推荐答案

通常,正如您已经提到的,实例方法是更好的做法.这并不是说静态方法是彻头彻尾的邪恶,它们只是具有不同而独特的目的.

In general, as you've already stated, instance methods are the better practice. That isn't to say that static methods are downright evil, they simply have a different and unique purpose.

重要的是 注意,在处理实例方法时,您使用的是 object ;而在使用静态方法时,则使用的是 class . >.使用静态方法时,您将无权访问实例通常可用的任何非静态属性.

It is important to note that when dealing with instance methods you are working with an object whereas with static methods you are working with a class. When using static methods you will not have access to any of your non-static properties that would normally be available with an instance.

以以下代码为例:

class Foo
{
    private $bar;
    private static $tavern;

    public function changeBar($value)
    {
        $this->bar = $value;
    }

    public function getBar()
    {
        return $this->bar;
    }

    public static function changeTavern($value)
    {
        self::$tavern = $value;
    }

    public static function getTavern()
    {
        return self::$tavern;
    }
}

Foo具有静态属性$tavern和非静态属性$bar.

Class Foo has a static property $tavern and a non-static property $bar.

如果创建了Foo的实例,则该对象的所有属性和方法均可用.

If an instance of Foo is created then all properties and methods are available to that object.

如果静态引用了Foo,则 class 仅可使用$tavern属性,changeTavern()方法和getTavern()方法.

If Foo is referenced statically then only the $tavern property, changeTavern() method and getTavern() method are available to the class.

让我们看下面的代码:

$foo = new Foo();
$foo->changeBar('Tipsy Turvy');
echo $foo->getBar(); // prints Tipsy Turvy

由于$fooFoo的实例,因此它可以访问整个类.调用changeBar()将修改$bar属性.直接从静态方法更改$bar属性将触发错误,因为可以用于对象,而不能用于 class .

Since $foo is an instance of Foo, it has access to the entire class. Calling changeBar() will modify the $bar property. To change the $bar property directly from a static method will trigger an error since $bar is available to the object and not the class.

// Calling this method would trigger an error
public static function changeBar($value)
{
    $this->bar = $value; // PHP will crash and burn if you try this.
}

如果要从静态方法访问类属性,则还必须将这些属性声明为静态.这将在以上类的上下文中起作用.您还将注意到,Foo的实例在读取静态属性时没有问题.

If you want to access class properties from static methods those properties must also be declared static. This will work in the context of the class above. You'll also note that an instance of Foo has no problem reading static properties.

Foo::changeTavern('Stumble Inn');
echo Foo::getTavern(); // prints Stumble Inn
echo $foo->getTavern(); // also prints Stumble Inn

关于静态代码,要记住的另一件事是它的行为不像实例.生成Foo的第一个实例时,属性$bar$tavern都没有值.如果要创建Foo的另一个实例,则会发现这些属性中只有一个不再包含值. (我确定您现在可以猜到哪个了.)

Another thing to remember about static code is that it doesn't behave like an instance. When the first instance of Foo was built both properties $bar and $tavern had no value. If you were to create another instance of Foo you would find that only one of those properties no longer contains a value. (I'm sure by now you can guess which one.)

$anotherFoo = new Foo();
echo $anotherFoo->getBar(); // prints nothing
echo $anotherFoo->getTavern(); // prints Stumble Inn
echo Foo::getTavern(); // prints Stumble Inn

同样,静态代码意味着您正在直接使用 class -实例意味着您正在使用 object .重要的是要注意,您编写的打算对其具有某种状态的任何类型的类都应用作实例.

So again, static code means you are working directly with a class - instances mean you are working with an object. It is important to note that any type of class you write that intends to have some kind of state to it should be used as an instance.

静态类可能很难调试和测试.测试可能会有问题,因为创建新实例时静态属性不会更改.调试也可能很困难,因为在一个类的所有实例中,静态属性的值都是相同的.进行一次更改,您将在所有这些更改中进行更改.跟踪导致更改的实例非常麻烦.

Static classes can be a little difficult to debug and test. Testing can be problematic because static properties don't change when you create a new instance. Debugging can also be difficult since the value of a static property is the same across all instances of an class. Make a change in one and you will make that change in all of them. Tracking down which instance made the change is a pain.

隐喻地说,仅在必要时使用糖等静态类.

Speaking metaphorically, use static classes like sugar - sparingly and only when necessary.

希望可以使您对该主题有所了解.

Hope that helps shed some light on the topic.

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

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