note 37698 deleted from language.oop by aidan
| From: | aidan@php.net | Date: | Sat, 03 Jul 2004 14:50:04 +0000 |
| Subject: | note 37698 deleted from language.oop by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72439@lists.php.net to get a copy of this message | ||
Note Submitter: natedone@hotmail.com
----
Not being able to use static class properties is a bit annoying. Here are my findings.
One way is to use a global variable. This is a huge hack job in my opinion. Example posted below
by "jp at antz29 dot com".
Another way to do it is to use a variable declared static in the class constructor function, then
store a reference to that static variable in an instance variable. This will give you a shared
variable clas wide, but is only accessible through an instance of that class. An example of this
implementation is posted below by "lachtan".
If those two solutions aren't quite perfect for you, I did find another way. Since we have
static variables for functions, exploit that to do your bidding. The drawback is that you must use
the same function to set your property as to retrieve it, but beyond that it is the most complete
solution. Example:
class SomeExample {
function echoMessage () {
echo SomeExample::exampleMessage();
}
function exampleMessage ( $text = null ) {
static $message;
if ( $text != null ) $message = $text;
return $message;
}
}
SomeExample::exampleMessage( "Hello world<br>" );
$instance = new SomeExample();
$instance->echoMessage();
SomeExample::exampleMessage( "Hello again<br>" );
echo SomeExample::exampleMessage();