This is a solution for those that still need to write code compatible with php 4 but would like to use the flexibility of static variables. PHP 4 does not support static variables within the class scope but it does support them within the scope of class methods. The following is a bit of a workaround to store data in static mode in php 4.
Note: This code also works in PHP 5.
(Tested on version 4.3.1+)
The tricky part is when using when arrays you have to do a bit of fancy coding to get or set individual elements in the array. The example code below should show you the basics of it though.
<?php
class StaticSample
{
//Copyright Michael White (www.crestidg.com) 2007
//You may use and modify this code but please keep this short copyright notice in tact.
//If you modify the code you may comment the changes you make and append your own copyright
//notice to mine. This code is not to be redistributed individually for sale but please use it as part
//of your projects and applications - free or non-free.
//Static workaround for php4 - even works with arrays - the trick is accessing the arrays.
//I used the format s_varname for my methods that employ this workaround. That keeps it
//similar to working with actual variables as much as possible.
//The s_ prefix immediately identifies it as a static variable workaround method while
//I'm looking thorugh my code.
function &s_foo($value=null, $remove=null)
{
static $s_var; //Declare the static variable. The name here doesn't matter - only the name of the method matters.
if($remove)
{
if(is_array($value))
{
if(is_array($s_var))
{
foreach($value as $key => $data)
{
unset($s_var[$key]);
}
}
}
else
{
//You can't just use unset() here because the static state of the variable will bring back the value next time you call the method.
$s_var = null;
unset($s_var);
}
//Make sure that you don't set the value over again.
$value = null;
}
if($value)
{
if(is_array($value))
{
if(is_array($s_var))
{
//$s_var = array_merge($s_var, $value); //Doesn't overwrite values. This adds them - a property of the array_merge() function.
foreach($value as $key => $data)
{
$s_var[$key] = $data; //Overwrites values.
}
}
else
{
$s_var = $value;
}
}
else
{
$s_var = $value;
}
}
return $s_var;
}
}
echo "Working with non-array values.<br>";
echo "Before Setting: ".StaticSample::s_foo();
echo "<br>";
echo "While Setting: ".StaticSample::s_foo("VALUE HERE");
echo "<br>";
echo "After Setting: ".StaticSample::s_foo();
echo "<br>";
echo "While Removing: ".StaticSample::s_foo(null, 1);
echo "<br>";
echo "After Removing: ".StaticSample::s_foo();
echo "<hr>";
echo "Working with array values<br>";
$array = array(0=>"cat", 1=>"dog", 2=>"monkey");
echo "Set an array value: ";
print_r(StaticSample::s_foo($array));
echo "<br>";
//Here you need to get all the values in the array then sort through or choose the one(s) you want.
$all_elements = StaticSample::s_foo();
$middle_element = $all_elements[1];
echo "The middle element: ".$middle_element;
echo "<br>";
$changed_array = array(1=>"big dog", 3=>"bat", "bird"=>"flamingo");
echo "Changing the value: ";
print_r(StaticSample::s_foo($changed_array));
echo "<br>";
//All you have to do here is create an array with the keys you want to erase in it.
//If you want to erase all keys then don't pass any array to the method.
$element_to_erase = array(3=>null);
echo "Erasing the fourth element: ";
$elements_left = StaticSample::s_foo($element_to_erase, 1);
print_r($elements_left);
echo "<br>";
echo "Enjoy!";
?>
Gültigkeitsbereichsoperator (::)
Der Gültigkeitsbereichsoperator (auch Paamayim Nekudotayim genannt) oder in einfacheren Worten der Doppel-Doppelpunkt ist ein Kürzel, das Zugriff auf statische, konstante und überschriebene Member oder Methoden einer Klasse erlaubt.
Wenn Sie diese Elemente außerhalb der Klassendefinition ansprechen, benutzen Sie den Namen der Klasse.
Beginnend mit PHP 5.3.0 ist es möglich eine Variable als Klassenreferenz zu nutzen. Der Variablenwert kann kein Schlüsselwort (wie self, parent oder static) sein.
Paamayim Nekudotayim scheint auf den ersten Blick eine eigenartige Wahl für die Benennung eines Doppel-Doppelpunktes zu sein. Jedoch entschied sich das Zend Team ihn so zu nennen, während Sie die Zend Engine 0.5 schrieben (welche PHP 3 antreibt). Tatsächlich bedeutet das Doppel-Doppelpunkt - in Hebräisch.
Beispiel #1 :: außerhalb der Klassendefinition
<?php
class MyClass {
const CONST_VALUE = 'Ein konstanter Wert';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // Ab PHP 5.3.0
echo MyClass::CONST_VALUE;
?>
Die drei speziellen Schlüsselwörter self, parent und static werden benutzt, um auf Member und Methoden von innerhalb der Klassendefinition zuzugreifen.
Beispiel #2 :: innerhalb der Klassendefinition
<?php
class OtherClass extends MyClass
{
public static $my_static = 'statische var';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
}
$classname = 'OtherClass';
echo $classname::doubleColon(); // Ab PHP 5.3.0
OtherClass::doubleColon();
?>
Wenn eine abgeleitete Klasse die Definition der Methode eines Vaters überschreibt, wird PHP die Methode des Vaters nicht aufrufen. Es obliegt der abgeleiteten Klasse, ob die Methode der Vaterklasse augerufen wird oder nicht. Dies gilt ebenfalls für Konstruktoren und Destruktoren, Überladung und magische Methodendefinitionen.
Beispiel #3 Eine Vatermethode aufrufen
<?php
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
class OtherClass extends MyClass
{
// Die Definition des Vaters überschreiben
public function myFunc()
{
// Aber dennoch die Funktion des Vaters aufrufen
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
}
$class = new OtherClass();
$class->myFunc();
?>
Siehe auch ein Beispiel zur Bedeutung von $this, wenn nich statische Methoden statisch aufgerufen werden.
Gültigkeitsbereichsoperator (::)
13-Feb-2007 09:11
22-Sep-2006 04:40
``self'' and ``parent'' are valid arguments to the ``new'' operator. All the constructor stuff works as expected. Example:
<?php
class Foo {}
class Bar extends Foo {
static function test() {
return(
array(
new self,
new self(),
new parent( 'a', 1 ),
) );
}
}
print_r( Bar::test() ); ?>
Calling Bar::test() gives you nice list of three instances. ( i'm describing it there, as it was a suprise to me. ) Maybe you can find it usefull, for example, in some generic ::__clone() method that works across inheritance tree.
27-Jan-2006 12:57
You use 'self' to access this class, 'parent' - to access parent class, and what will you do to access a parent of the parent? Or to access the very root class of deep class hierarchy? The answer is to use classnames. That'll work just like 'parent'. Here's an example to explain what I mean. Following code
<?php
class A
{
protected $x = 'A';
public function f()
{
return '['.$this->x.']';
}
}
class B extends A
{
protected $x = 'B';
public function f()
{
return '{'.$this->x.'}';
}
}
class C extends B
{
protected $x = 'C';
public function f()
{
return '('.$this->x.')'.parent::f().B::f().A::f();
}
}
$a = new A();
$b = new B();
$c = new C();
print $a->f().'<br/>';
print $b->f().'<br/>';
print $c->f().'<br/>';
?>
will output
[A] -- {B} -- (C){C}{C}[C]
25-Nov-2005 12:08
In response to ian at [first name]henderson dot org:
(related bogus bug report: http://bugs.php.net/bug.php?id=26930)
The functionality you've expected maybe will be possible in PHP6, probably by using the static keyword in conjunction with the scope resolution parameter. You can read more about this in the minutes of the PHP developers meeting at 11 and 12 november in Paris: http://www.php.net/~derick/meeting-notes.html point 5.4: Late static binding using "this" without "$" (or perhaps with a different name)
07-Sep-2005 06:47
The alternative to john at johnjosephbachir dot org's suggestion without using eval():
call_user_func(array($classname, 'a_static_function'));
31-Aug-2005 12:23
A way to achieve Doug's last example
<?php
//this doesn't work
$classname::a_static_function();
?>
is to use eval():
<?php
//this works
$string = $classname.'::a_static_function();';
eval($sting);
?>
Remember to include a semicolon inside the string passed to eval().
29-Aug-2005 04:52
Just a note that while this works for new objects:
<?php
$obj = new $classname();
?>
and this works to call a function:
<?php
$functionname();
?>
this does not work to call static functions, you will get a parse error:
<?php
$classname::a_static_function();
?>
29-Apr-2005 02:58
In response to ian at [first name]henderson dot org:
You are not allowed to redefine static methods, see
http://www.php.net/manual/en/language.oop5.static.php
And in response to thenewparadigm at hotmail dot com: the behaviour you describe seems appropriate for *classes* with static variables, see "Using static variables" on http://nl2.php.net/static
05-Mar-2005 06:43
There is also a quirk with using the scope resolution operator on static class variables. Below is an example using a highly modified version of Ian's code:
<?php
class ExampleSuperclass
{
static $className;
static function showClassName() {
echo self::$className . "\n";
}
}
class ExampleSubclassOne extends ExampleSuperclass
{
static function setClassName()
{
self::$className = "subclassOne";
}
}
class ExampleSubclassTwo extends ExampleSuperClass
{
static function setClassName()
{
self::$className = "subclassTwo";
}
}
// setting variables for each class
ExampleSubclassOne::setClassName();
ExampleSubclassTwo::setClassName();
ExampleSubclassOne::showClassName(); // output is "subclassTwo"!
// more output:
echo ExampleSubclassOne::$className . "\n"; // output is "subclassTwo"!
echo ExampleSubclassTwo::$className . "\n"; // output is "subclassTwo"
echo ExampleSuperclass::$className . "\n"; // output is "subclassTwo"!
?>
appearantly, any static variables defined in a superclass are directly referenced in subclasses,
and all changes are visible throughout the class heirarchy. care must be taken when using static
class variables.
01-Feb-2005 07:43
Please note that methods called by the scope resolution operator which are defined by a superclass of the first operand are called in the scope of the SUPERCLASS. For example,
<?php
class ExampleSuperclass
{
static function classType()
{
return "superclass";
}
static function doSomething()
{
echo "doing something with " . self::classType();
}
}
class ExampleClass extends ExampleSuperclass
{
static function classType()
{
return "subclass";
}
}
ExampleClass::doSomething();
// output is "doing something with superclass"!
?>
This can be surprising (it surprised me!) when coming from other object-oriented languages, which would output "doing something with subclass" in this case.