when attempting to implement a singleton class, one might also want to either
a) disable __clone by making it private
b) bash the user who attempts to clone by defining __clone to throw an exception
static キーワード
クラスメンバもしくはメソッドを static として宣言することで、 クラスのインスタンス化の必要なしにアクセスすることができます。 static なメンバは、インスタンス化されたクラスオブジェクトから アクセスすることはできません (static なメソッドからは可能です) 。
PHP 4 との互換性を維持するため、 可視性の宣言が ない場合、そのメンバまたはメソッドは、 publicとして宣言されていると みなされます。
static メソッドは、オブジェクトのインスタンスを生成せずに コールすることができます。疑似変数 $this は、 static として宣言されたメソッドの内部から利用することはできません。
static プロパティは、矢印演算子 -> によりオブジェクトからアクセス することはできません。
static でないメソッドを静的にコールすると、E_STRICT レベルの 警告が発生します。
PHP 5.3.0 以降では、変数を用いてクラスを参照することも可能です。 変数の値に (self や parent、 static といった) キーワードを指定することはできません。
例1 static メンバの例
<?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"; // Undefined "Property" my_static
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // PHP 5.3.0 以降で対応
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
例2 static メソッドの例
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // PHP 5.3.0 で対応
?>
static キーワード
vvikramraj at yahoo dot com
24-Sep-2008 06:24
24-Sep-2008 06:24
wbcarts at juno dot com
11-Sep-2008 11:53
11-Sep-2008 11:53
A CLASS WITH MEAT ON IT'S BONES...
I have yet to see an example that I can really get my chops into. So I am offering an example that I hope will satisfy most of us.
class RubberBall
{
/*
* ALLOW these properties to be inherited TO extending classes - that's
* why they're not private.
*
* DO NOT ALLOW outside code to access with 'RubberBall::$property_name' -
* that's why they're not public.
*
* Outside code should use:
* - RubberBall::getCount()
* - RubberBall::setStart()
* These are the only routines outside code can use - very limited indeed.
*
* Inside code has unlimited access by using self::$property_name.
*
* All RubberBall instances will share a "single copy" of these properties - that's
* why they're static.
*/
protected static $count = 0;
protected static $start = 1;
protected static $colors = array('red','yellow','blue','orange', 'green', 'white');
protected static $sizes = array(4, 6, 8, 10, 12, 16);
public $name;
public $color;
public $size;
public function __construct(){
$this->name = 'RB_' . self::$start++;
$this->color = (int) rand(0, 5);
$this->size = self::$sizes[(int) rand(0, 5)];
self::$count++;
}
public static function getCount(){
return self::$count;
}
public static function setStart($val){
self::$start = $val;
}
/*
* Define the sorting rules for RubberBalls - which is to sort by self::$colors.
* PHP's usort() method will call this function many many times.
*/
public static function compare($a, $b){
if($a->color < $b->color) return -1;
else if($a->color == $b->color) return 0;
else return 1;
}
public function __toString(){
return "RubberBall[
name=$this->name,
color=" . self::$colors[$this->color] . ",
size=" . $this->size . "\"]";
}
}
# RubberBall counts the number of objects created, but allows us to
# set the starting count like so:
RubberBall::setStart(100);
# create a PHP Array and initialize it with (12) RubberBall objects
$balls = array();
for($i = 0; $i < 12; $i++) $balls[] = new RubberBall();
# sort the RubberBall objects. PHP's usort() calls RubberBall::compare() to do this.
usort($balls, array("RubberBall", "compare"));
# print the sorted results - uses the static RubberBall::getCount().
echo 'RubberBall count: ' . RubberBall::getCount() . '<br><br>';
foreach($balls as $ball) echo $ball . '<br>';
I'm running out of room so I have not displayed the output, but it is tested and it works great.
Mathijs Vos
23-Aug-2008 06:53
23-Aug-2008 06:53
<?php
class foo
{
public static $myStaticClass;
public function __construct()
{
self::myStaticClass = new bar();
}
}
class bar
{
public function __construct(){}
}
?>
Please note, this won't work.
Use self::$myStaticClass = new bar(); instead of self::myStaticClass = new bar(); (note the $ sign).
Took me an hour to figure this out.
nacion_01 at hotmail dot com
23-Aug-2008 12:38
23-Aug-2008 12:38
Thanks to all of you, this is my little contribution, i hope it give you ideas:
The next script uses a class Logs to store and show the logs. You can show the last logs of your scripts. You can:
- push ("a new log")
- show () // the last logs until the las call to show()
- showAll() // to show all the logs and
- message("a forced message") // which sends a message to the log output with out storing the message (you may imagine other solutions)
<?php
class Logs{
// this makes this thing to be hidden from outside calls [private]
private static $logs=array("start");
private static $index=0;
// this is available for outside calls [public]
public static function getLogsCount(){
// the constant "self" refers to the class, instead $this wich
// refers to an instance of a class (this class is not instanced,
// its running itself, so lets use "self" and "::" to access the
// "static" functions and properties.
$count=count(self::$logs);
return $count;
}
public static function show(){
//echo ("# Logs::show ".self::getLogsCount());
$count=self::getLogsCount();
// start the show since the last log shown ($index)
for ($i=self::$index; $i<$count;$i++){
$log=self::$logs[$i];
self::message($log);
}
// update the las log shown index
self::$index=$i;
}
public static function showAll(){
//echo ("# Logs::show ".self::getLogsCount());
// show the logs from cero to last
// im starting with php so I use for (...) because its my
// way in AS3.0 but you better keep checking that
$count=self::getLogsCount();
for ($i=0; $i<$count;$i++){
$log=self::$logs[$i];
self::message($log);
}
}
// Yup! a new log!
public static function push($msg){
//self::message("# Logs::push ");
array_push(self::$logs, $msg);
}
// Send a message to the html output.
// Now, a little words about this one, you may want to use an
// special button on your html or somewhere else to show or hide the
// logs, because you are in development and want to keep this thing
// little hidden of your clients. Probably may generate a java script
// variable so instead of [echo "<p>...</p>";] is ok to
// use [echo "<script>logs+=$msg</script>"]
// I dont know, figure it out.
public function message($msg){
echo "<p>- Logs > $msg </p>";
}
}
// Ready to use.
// to call a function in an static class, refer to the function as
// TheClass::theFunction
// remember, php uses :: (sorry, I still learning why because I come from the AS3 world)
Logs::push("log1");
Logs::show();
Logs::push("log2");
Logs::show();
Logs::push("log3");
Logs::showAll();
?>
michaelnospamdotnospamdaly at kayakwiki
14-Jul-2008 09:59
14-Jul-2008 09:59
Further to the comment by "erikzoltan NOSPAM at msn NOSPAM dot com" on 05-Apr-2005 03:40,
It isn't just constructors that can't be used for static variable initialization, it's functions in general:
class XYZ
{
static $foo = chr(1); // will fail
}
You have to do external initialization:
XYZ::$foo = chr(1);
class XYZ
{
static $foo;
}
Siarhei
04-Mar-2008 09:25
04-Mar-2008 09:25
There is a problem to make static property shared only for objects of self class not defining it in every child class.
Example:
class a
{
public static $s;
public function get()
{
return self::$s;
}
}
class b extends a { }
class c extends b { }
a::$s = 'a';
$c = new c();
echo $c->get(); // a
There is solution i found:
class a
{
public final function v($vs = null)
{
static $s = null;
if(!is_null($vs))
$s = $vs;
return $s;
}
}
class b extends a { }
class c extends b { }
$a = new a();
$a->v('a');
$aa = new a();
$aa->v('last a');
$c = new c();
$c->v('c');
echo $a->v().' - '.$c->v(); // last a - c
inkredibl
28-Jan-2008 04:27
28-Jan-2008 04:27
Note that you should read "Variables/Variable scope" if you are looking for static keyword use for declaring static variables inside functions (or methods). I myself had this gap in my PHP knowledge until recently and had to google to find this out. I think this page should have a "See also" link to static function variables.
http://www.php.net/manual/en/language.variables.scope.php
ssj dot narutovash at gmail dot com
02-Jan-2008 12:48
02-Jan-2008 12:48
It's come to my attention that you cannot use a static member in an HEREDOC string. The following code
class A
{
public static $BLAH = "user";
function __construct()
{
echo <<<EOD
<h1>Hello {self::$BLAH}</h1>
EOD;
}
}
$blah = new A();
produces this in the source code:
<h1>Hello {self::}</h1>
Solution:
before using a static member, store it in a local variable, like so:
class B
{
public static $BLAH = "user";
function __construct()
{
$blah = self::$BLAH;
echo <<<EOD
<h1>Hello {$blah}</h1>
EOD;
}
}
and the output's source code will be:
<h1>Hello user</h1>
gabe at mudbugmedia dot com
16-Oct-2007 09:55
16-Oct-2007 09:55
Currently, inheritance with static methods can be a difficult matter, mainly because the 'self' keyword refers the exact class in which the reference is present, much like __CLASS__. Thus, in a case like the following:
<?php
class A {
static function foo () {
echo "A::foo()";
}
static function callFoo () {
self::foo();
}
}
class B extends A {
static function foo () {
echo "B::foo()";
}
}
B::callFoo();
?>
Will output 'A::foo()' instead of 'B::foo()'. As of 5.2 and below, there is no clean way to get around this. get_class(), self, __CLASS__, and even the stack (examined by debug_backtrace()) will all report the appropriate class as being 'A', which is resolved before being called. There is no sane way, short of determining the filename and line of code from debug_backtrace, re-opening the file, and parsing that out yourself (this kludge is left as an exercise to only the most demented developer). This setup makes it extremely difficult to develop an ORM like ActiveRecord in PHP.
You'll see some rather ugly solutions below, including passing the class name to callFoo(). Another possible route is to skip static calls all together and make a function that returns an instance of the object, thus allowing you to gain access to the '$this' variable (which *will* know the class name that was originally being called):
<?php
class A {
static function foo () {
echo "A::foo()";
}
static function callFoo () {
$this->foo();
}
}
class B extends A {
static function foo () {
echo "B::foo()";
}
}
function getB () { return new B(); }
getB()->callFoo();
?>
Luckily, a work around has been committed to PHP's HEAD, and should be available in the 5.3 series. The concept is called "late static binding" (LSB), and can be read about at http://www.colder.ch/news/08-24-2007/28/late-static-bindings-expl.html . Basically, you'd have the exact same setup in as in the first example, but instead of calling "self::foo()", you instead would call "static::foo()".
Clment Genzmer
09-Aug-2007 04:54
09-Aug-2007 04:54
The best solution I found for the non-inherited static problem : pass the name of the class.
<?php
class A {
public static $my_vars = "I'm in A";
static function find($class) {
$vars = get_class_vars($class) ;
echo $vars['my_vars'] ;
}
}
class B extends A {
public static $my_vars = "I'm in B";
}
B::find("B");
// Result : "I'm in B"
?>
james at earthemergency dot org
12-Oct-2006 07:36
12-Oct-2006 07:36
One way to get around the fact that static variables are shared throughout the inheritance tree to map a instance variable to a global variable named after the class.
<?php
$GLOBALS['static'] = array();
class root {
public $self;
public static $all_instances;
function __construct() {
$this->self = &$GLOBALS['static'][get_class($this)];
$this->self['instances'] += 1;
self::$all_instances += 1;
}
}
class child_a extends root {
}
class child_b extends root {
}
$a1 = new child_a();
$a2 = new child_a();
$a3 = new child_a();
$b1 = new child_b();
$b2 = new child_b();
echo "child_a instances: " . $a3->self['instances'] . " | all_instances: " . child_a::$all_instances . "\n";
echo "child_b instances: " . $b2->self['instances'] . " | all_instances: " . child_b::$all_instances . "\n";
echo "\$GLOBALS['static'] = ";
var_dump($GLOBALS['static']);
?>
// Output:
child_a instances: 3 | all_instances: 5
child_b instances: 2 | all_instances: 5
$GLOBALS['static'] = array(2) {
["child_a"]=>
&array(1) {
["instances"]=>
int(3)
}
["child_b"]=>
&array(1) {
["instances"]=>
int(2)
}
}
jan(dot)-re-mov.ethis-mazanek/AT-abeo.cz
20-Sep-2006 02:42
20-Sep-2006 02:42
This reacts to comment from
michael at digitalgnosis dot removethis dot com from 16-Dec-2004 08:09
> 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.)
In my test on Windows PHP Version 5.1.4 it seems that it *is possible* to override static method.
This code works at my machine without producing E_STRICT error:
<?php
class Base
{
static function Foo ( $class = __CLASS__ )
{
call_user_func(array($class,'Bar'));
}
}
class Derived extends Base
{
static function Foo ( $class = __CLASS__ )
{
parent::Foo($class);
}
static function Bar ()
{
echo "Derived::Bar()";
}
}
Derived::Foo(); // This time it works.
?>
Jakob Schwendner
04-Nov-2005 05:17
04-Nov-2005 05:17
Here is my solution to the static search method problem for data objects. I found the debug_trace version posted earlier quite clever, but a little too risky.
<?php
class Foo {
static function find($class) {
$obj = new $class();
return $obj;
}
}
class Bar extends Foo {
static function find() {
return parent::find(__CLASS__);
}
function print_hello() {
echo("hello");
}
}
Bar::find()->print_hello();
?>
aidan at php dot net
04-May-2005 10:14
04-May-2005 10:14
To check if a function was called statically or not, you'll need to do:
<?php
function foo () {
$isStatic = !(isset($this) && get_class($this) == __CLASS__);
}
?>
More at (http://blog.phpdoc.info/archives/4-Schizophrenic-Methods.html).
(I'll add this to the manual soon).
06-Apr-2005 06: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
06-Apr-2005 08:50
06-Apr-2005 08: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
06-Apr-2005 06:40
06-Apr-2005 06: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
// This doesn't work.
class XYZ
{
// The following line will throw a syntax error.
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
// This will work.
// Moved the static variable's initialization logic outside the class.
XYZ::$ABC = new ABC();
class XYZ
{
// I'm just declaring the static variable here, but I'm not initializing it.
public static $ABC;
}
class ABC
{
}
$myXyz = new XYZ();
var_dump($myXyz);
var_dump(XYZ::$ABC);
?>
michalf at ncac dot torun dot pl
01-Apr-2005 06:42
01-Apr-2005 06: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; //one would naively expect "foo"...
?>
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
15-Jan-2005 05:57
15-Jan-2005 05: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 11:11
14-Jan-2005 11: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(); // prints array(2) { ["key1"]=> int(1) ["key2"]=> int(2) }
new bad();
new bar(); // prints bool(false)
?>
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(); // prints array(2) { ["key1"]=> int(1) ["key2"]=> int(2) }
new bad(); // results in a fatal error
?>
michael at digitalgnosis dot removethis dot com
16-Dec-2004 03:41
16-Dec-2004 03: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 03:09
16-Dec-2004 03: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(); // we want this to print "Derived::Bar()"
?>
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(); // This time it works.
?>
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
17-Nov-2004 01:10
17-Nov-2004 01: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); // returns the new object
$y = Singleton::getInstance();
var_dump($y); // returns the existing object
?>
ckj
dmintz at davidmintz dot org
10-Nov-2004 07:20
10-Nov-2004 07: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();
