简体   繁体   English

PHP,OOP,静态

[英]PHP, OOP, Static

I am studying PHP,OOP and i am at Static, At this php.net/static i didnt understand this sentence 我正在学习PHP,OOP和我在静态,在这个php.net/static我不明白这句话

Calling non-static methods statically generates an E_STRICT level warning.

I did understand it's Valid for methods only (not for Properties) by the sentence above, but i didn't succeed to understand It practically, I'm glad if anything could please show me code that explains the sentence above, Wishing you a pleasant week. 我确实理解它仅适用于上述句子中的方法(不适用于属性),但我没有成功理解它实际上,我很高兴如果有什么可以请告诉我解释上述句子的代码,祝你愉快周。

Here is an example of what they mean with the sentence you are asking about. 以下是您询问的句子的含义示例。

Consider the following class with one method (it is not static). 使用一种方法考虑以下类(它不是静态的)。

class Test
{
    function method()
    {
        echo "Hello from method";
    }
}

Test::method();  // attempt to statically call a non-static method

This is the output: 这是输出:

Strict Standards : Non-static method Test::method() should not be called statically in /obj.php on line 12 严格标准 :非静态方法Test :: method()不应在第12行的/obj.php中静态调用
Hello from method 方法你好

As you can see, it did execute the method when called static even though it is not a static method, however a strict error message was displayed. 正如你所看到的,它没有所谓的静态时,即使它不是一个静态方法,被显示。然而严格的错误信息执行的方法。

If the method method() referenced the keyword $this , then you would encounter a fatal error because $this does not exist in the context of a static method call. 如果方法method()引用了关键字$this ,那么您将遇到致命错误,因为$this在静态方法调用的上下文中不存在。 So while it is technically possible to call a non-static class method statically, it should not be done. 因此,虽然技术上可以静态地调用非静态类方法,但是不应该这样做。

EDIT: 编辑:

The reason you are even allowed to call a non-static class member statically is because the static keyword did not exist in PHP4 in the context of class methods so if you were designing a static class or method in PHP4, there was no keyword to indicate it, you would simply call it in the static fashion. 您甚至被允许静态调用非静态类成员的原因是因为在类方法的上下文中PHP4中不存在static关键字,因此如果您在PHP4中设计静态类或方法,则没有关键字来指示它,你只需要以静态方式调用它。 Now PHP5 emits the warning if the method is called statically but doesn't have the static keyword in the declaration. 现在PHP5会在静态调用方法但是在声明中没有static关键字时发出警告。

class Foo
{
    public static $my_static = 'foo';
    public $my_non_static = 'bar';

    public function staticValue() {
        return self::$my_static;
    }

    public function nonStaticValue() {
        return self::$my_non_static;
    }
}

print Foo::$my_static . "\n"; // OK
print Foo::staticValue(). "\n"; // E_STRICT

print Foo::$my_non_static . "\n"; // Fatal
print Foo::nonStaticValue(). "\n"; // Fatal

print Foo::$my_static . "\\n"; is OK - static property accessed statically. 没关系 - 静态访问静态属性。

print Foo::staticValue(). "\\n"; gives E_STRICT - non-static method accessed statically, but not Fatal error, because this method doesn't access non-static properties. 给出E_STRICT - 静态访问的非静态方法,但不是致命错误,因为此方法不访问非静态属性。

Other two give Fatal error because non-static field cannot be accessed statically. 其他两个给出致命错误,因为无法静态访问非静态字段。

It's because even if you can call non-static methods statically, you shouldn't and it will be logged. 这是因为即使您可以静态调用非静态方法,也不应该记录它。

class Foo {
    function bar(){
        print "you should not do that";
    }
} 

Foo::bar(); would actually works, but you will get a E_STRICT warning because you can do that, but you shouln't . 实际上工作,但你会得到一个E_STRICT警告,因为你做到这一点,但你shouln't。

If a method is non-static, it means that it belongs to an instance of a class. 如果方法是非静态的,则意味着它属于类的实例。 For example, if we have a class Car with a method called getDamage() (which computes how much damaged the car is), then you should not call this method in a static way. 例如,如果我们有一个Car类,其中有一个名为getDamage()的方法(计算汽车损坏程度),那么就不应该以静态方式调用此方法。

You should only create an instance of the Car class and call getDamage() on that instance. 您应该只创建Car类的实例并在该实例上调用getDamage() This makes sense because a particular car can be damaged for 25% while another car can be damaged for 70%. 这是有道理的,因为特定的汽车可能会损坏25%,而另一辆汽车可能会损坏70%。

But calling getDamage() in a static way makes no sense: a static method does not belong to a particular instance of the class but to the class itself. 但是以静态方式调用getDamage()是没有意义的:静态方法不属于类的特定实例,而属于类本身。 And a Car class has no useful way of giving a result for getDamage() . 并且Car类没有为getDamage()提供结果的有用方法。 You could still compute a value (perhaps 0 ) but it does not make sense. 你仍然可以计算一个值(可能是0 )但它没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM