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

Posted by Peter Pei on 01/09/08 04:02

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);
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();
?>

 

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

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