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

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.

最佳回答
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 . "
"; // OK
print Foo::staticValue(). "
"; // E_STRICT

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

print Foo::$my_static . " "; is OK - static property accessed statically.

print Foo::staticValue(). " "; gives E_STRICT - non-static method accessed statically, but not Fatal error, because this method doesn t access non-static properties.

其它两个则给出致命错误, 因为非静态字段无法静态访问 。

问题回答

这是一个例子 他们的意思 与你要求的判刑。

采用一种方法考虑以下类别(不是静态的)。

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

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

这是输出 :

Strict Standards: Non-static method Test::method() should not be called statically in /obj.php on line 12
Hello from method

正如您所看到的,它did 执行称为静态的方法, 尽管它不是一个静态方法, 但是它显示的是一个严格的错误信息 。

如果方法 method () 引用了关键词 $$ this ,那么你会遇到致命错误,因为“code>$$ this 在静态方法调用中不存在。因此,虽然技术上可以静态调用非静态类方法,但不应该这样做。

编辑:

甚至允许您固定调用非静态类成员的原因是, PHP4 中的静态关键字在类方法中不存在,所以如果您在 PHP4 中设计静态类别或方法,那么没有关键字可以表示它,您只需以静态方式称呼它。现在 PHP5 发布警告,如果该方法被固定命名,但在声明中没有静态关键字 。

这是因为即使你可以静态地调用非静态方法,你还是应该t,它也会被记录下来。

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

Foo::bar (); 将实际有效,但您将得到一个“强”E_STRICT

如果一种方法不是静态的,它意味着它属于一个类的例子。例如,如果我们有一个类 car ,其方法叫做 getDamage () (该方法计算了汽车受到的损坏程度),那么您就不应该用静态的方式称呼这个方法。

您只应该创建一个 < code> Car 类实例, 并在此实例中调用 < code> getDamage () < (/ code) > 。 这有道理, 因为某辆车可能损坏25%, 而另一辆车可能损坏70% 。

但以静态方式调用getDamage () () ) 毫无意义: 静态方法不属于该类的特定实例, 而是属于该类本身。 而一个 Car 类无法为 getDamage () () 带来结果。 您仍然可以计算一个值( 也许 < code> 0 ), 但是它没有意义 。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签