|
|
 |
Declaring class members or methods as static, makes them callable
from outside the object context. A member or method declared
with static can not be accessed with a variable that is an instance
of the object and cannot be re-defined in an extending class.
The static declaration must be after the visibility declaration. For
compatibility with PHP 4, if no visibility
declaration is used, then the member or method will be treated
as if it was declared as public static.
Because static methods are callable without an instance of
the object created, the pseudo variable $this is
not available inside the method declared as static.
In fact static method calls are resolved at compile
time. When using an explicit class name the method is already identified
completely and no inheritance rules apply. If the call is done by
self then self is translated to
the current class, that is the class the code belongs to. Here also no
inheritance rules apply.
Static properties cannot be accessed through the object using the arrow
operator ->.
Example 19-13. Static member example |
<?php
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n"; print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
|
|
Example 19-14. Static method example |
<?php
class Foo {
public static function aStaticMethod() {
}
}
Foo::aStaticMethod();
?>
|
|
User Contributed Notes
Static Keyword
aidan at php dot net
04-May-2005 09:14
06-Apr-2005 05:14
You misunderstand the meaning of inheritance : there is no duplication of members when you inherit from a base class. Members are shared through inheritance, and can be accessed by derived classes according to visibility (public, protected, private).
The difference between static and non static members is only that a non static member is tied to an instance of a class although a static member is tied to the class, and not to a particular instance.
That is, a static member is shared by all instances of a class although a non static member exists for each instance of class.
Thus, in your example, the static property has the correct value, according to principles of object oriented conception.
class Base
{
public $a;
public static $b;
}
class Derived extends Base
{
public function __construct()
{
$this->a = 0;
parent::$b = 0;
}
public function f()
{
$this->a++;
parent::$b++;
}
}
$i1 = new Derived;
$i2 = new Derived;
$i1->f();
echo $i1->a, ' ', Derived::$b, "\n";
$i2->f();
echo $i2->a, ' ', Derived::$b, "\n";
outputs
1 1
1 2
erikzoltan NOSPAM at msn NOSPAM dot com
05-Apr-2005 07:50
I was doing this in a more complex example (than previous note) and found that I had to place the initialization statement AFTER the class in a file where I was using the __autoload function.
erikzoltan NOSPAM at msn NOSPAM dot com
05-Apr-2005 05:40
I had trouble getting a static member to be an instance of a class. Here's a code example that DOESN'T work.
<?php
class XYZ
{
public static $ABC = new ABC();
}
class ABC
{
}
$myXyz = new XYZ();
var_dump($myXyz);
var_dump(XYZ::$ABC);
?>
I get the following entry in my error log.
[05-Apr-2005 18:27:41] PHP Parse error: syntax error, unexpected T_NEW in staticTest.php on line 7
Since PHP doesn't appear to allow static constructor methods, I was only able to resolve this problem by moving the initialization outside of the class. To make my code more self-documenting I put it above the class. The revised example below appears to work.
<?php
XYZ::$ABC = new ABC();
class XYZ
{
public static $ABC;
}
class ABC
{
}
$myXyz = new XYZ();
var_dump($myXyz);
var_dump(XYZ::$ABC);
?>
michalf at ncac dot torun dot pl
31-Mar-2005 04:42
Inheritance with the static elements is a nightmare in php. Consider the following code:
<?php
class BaseClass{
public static $property;
}
class DerivedClassOne extends BaseClass{
}
class DerivedClassTwo extends BaseClass{
}
DerivedClassOne::$property = "foo";
DerivedClassTwo::$property = "bar";
echo DerivedClassOne::$property; ?>
What would you expect as an output? "foo"? wrong. It is "bar"!!! Static variables are not inherited, they point to the BaseClass::$property.
At this point I think it is a big pity inheritance does not work in case of static variables/methods. Keep this in mind and save your time when debugging.
best regards - michal
c_daught_d at earthlink dot net
14-Jan-2005 03:57
A twist on christian at koch dot net's Singleton example is setting/getting non-static member variables using self::$instance->varname within static method calls.
Within the modified Singleton class below, the member variable $value is set within the getInstance static method instead of the constructor.
Whether this is "pure" OPP, I don't know. But it does work, is worth mentioning, and could be usefull.
class Singleton
{
private static $instance=null;
private $value=null;
private function __construct() {
}
public static function getInstance() {
if ( self::$instance == null ) {
echo "<br>new<br>";
self::$instance = new Singleton("values");
self::$instance->value = "values";
}
else {
echo "<br>old<br>";
}
return self::$instance;
}
}
ference at super_delete_brose dot co dot uk
14-Jan-2005 09:11
Both static and const fields can be accessed with the :: operator. However, while a constant can't be changed, this is not true for static variables.
If you want to access an array using the :: operator you have to declare the array static, since you can't have a constant array. Beware:
<?php
class foo
{
static $stuff = array('key1' => 1, 'key2' => 2);
}
class bar
{
public function __construct()
{
var_dump(foo::$stuff);
}
}
class bad
{
public function __construct()
{
foo::$stuff = FALSE;
}
}
new bar(); new bad();
new bar(); ?>
A safe implementation requires a little more effort:
<?php
class foo
{
private static $stuff = array('key1' => 1, 'key2' => 2);
public final static function getstuff()
{
return self::$stuff;
}
}
class bar
{
public function __construct()
{
var_dump(foo::getstuff());
}
}
class bad
{
public function __construct()
{
foo::$stuff = FALSE;
}
}
new bar(); new bad(); ?>
michael at digitalgnosis dot removethis dot com
16-Dec-2004 01:41
Here's another way to do the same thing (see my post below) without having to muck up your Foo() function's parameters in the Base and all Derived classes.
However, you cannot use static, and still must define Foo() in derived classes. This way also performs slower and may not always work--but it DOES make for prettier code.
<?php
class Base
{
function Foo ()
{
$call = debug_backtrace();
call_user_func(array($call[1]['class'],'Bar'));
}
}
class Derived extends Base
{
function Foo () { parent::Foo(); }
function Bar ()
{
echo "Derived::Bar()";
}
}
Derived::Foo();
?>
michael at digitalgnosis dot removethis dot com
16-Dec-2004 01:09
If you are trying to write classes that do this:
<?php
class Base
{
static function Foo ()
{
self::Bar();
}
}
class Derived extends Base
{
function Bar ()
{
echo "Derived::Bar()";
}
}
Derived::Foo(); ?>
Then you'll find that PHP can't (unless somebody knows the Right Way?) since 'self::' refers to the class which owns the /code/, not the actual class which is called at runtime. (__CLASS__ doesn't work either, because: A. it cannot appear before ::, and B. it behaves like 'self')
But if you must, then here's a (only slightly nasty) workaround:
<?php
class Base
{
function Foo ( $class = __CLASS__ )
{
call_user_func(array($class,'Bar'));
}
}
class Derived extends Base
{
function Foo ( $class = __CLASS__ )
{
parent::Foo($class);
}
function Bar ()
{
echo "Derived::Bar()";
}
}
Derived::Foo(); ?>
Note that Base::Foo() may no longer be declared 'static' since static methods cannot be overridden (this means it will trigger errors if error level includes E_STRICT.)
If Foo() takes parameters then list them before $class=__CLASS__ and in most cases, you can just forget about that parameter throughout your code.
The major caveat is, of course, that you must override Foo() in every subclass and must always include the $class parameter when calling parent::Foo().
christian at koch dot net
16-Nov-2004 11:10
STATIC is cool. Here is an example how to get an existing instance as a Singleton:
<?php
class Singleton {
private static $instance=null;
private $value=null;
private function __construct($value) {
$this->value = $value;
}
public static function getInstance() {
if ( self::$instance == null ) {
echo "<br>new<br>";
self::$instance = new Singleton("values");
} else {
echo "<br>old<br>";
}
return self::$instance;
}
}
$x = Singleton::getInstance();
var_dump($x); $y = Singleton::getInstance();
var_dump($y); ?>
ckj
dmintz at davidmintz dot org
09-Nov-2004 05:20
[Editor's Note: This is done for back compatability. Depending on your error level, An E_STRICT error will be thrown.]
PHP 5.0.1 doesn't seem to mind if you call a static method in a non-static context, though it might not be the best of style to do so.
On the other hand, PHP complains if you try to try to call a non-static method in a static context (if your error reporting is cranked up to E_STRICT).
class Test {
static function static_method() {
echo "Here's your static method: Foo!<br />\n";
}
function static_method_caller() {
echo "static_method_caller says: ";$this->static_method();
}
function non_static() {
echo "I am not a static method<br />\n";
}
}
$t = new Test();
$t->static_method();
$t->static_method_caller();
Test::non_static();
| |