You are here: Re: interesting but also dangerous static method in PHP « PHP Language « IT news, forums, messages
Re: interesting but also dangerous static method in PHP

Posted by Michael Fesser on 01/09/08 09:18

..oO(Peter Pei)

>The following code works, and that bothers me. Actually two things bothered
>me:
>1) Essentially PHP is not very strict about static and non-static. function
>say() is not declared as static, but it was written as if it is static: the
>use of self:: and accessing a static value, and it can actually be accessed
>as a static function (meaning not from an instance);

With error_reporting = E_ALL|E_STRICT you'll get a warning:

Strict Standards: Non-static method A::say() should not be called
statically in [...]

>2) As the code showed, function say() can be called from both the class (as
>if it is static) or from one of its instance (as if it is not static).
>
>Interesting...
>
><?php
> class A {
> public static $a = 12;
> public function say() {
> print self::$a . "\n";
> }
> }
>
> A::say();
> //the following does not work
> $a = new A();
> $a->say();
>?>

Both work here, except for the E_STRICT warning.

Micha

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация