I should have made a note of which version of php 5 I am currently using, but I didn't (I'm pretty sure it's 5.2.2).
Ayway, I just want to clarify that my understanding of the static keyword under php is correct, as I tried something earlier that I would have expected to work in any other OOP language, and it didn't.
Am I correct in stating:
a) Static class properties cannot be assigned values after declaration.
b) Static variables cannot be assigned an object data type, only primitives.
c) Static class properties cannot be accessed by an instantiated object through the $this notation.
I tried implementing something similar to the code below, which I would have expected to work, but it seems php handles static slightly differently to other languages.
Regards,
Rob.
Expand|Select|Wrap|Line Numbers
- class A
- {
- //Various instance variables and methods.
- }
- class B
- {
- //Did not have a problem with either of the below lines.
- protected static var;
- protected static flag = false;
- //Did have a problem with this though:
- //protected static var = new A;
- public function __construct()
- {
- if(!self::flag) {
- //Had a problem with both the following lines.
- self::var = new A;
- self::flag = true;
- }
- }
- }
- class C extends B
- {
- public function __construct()
- {
- parent::__construct();
- }
- }