timgolding
02-16-2012, 09:07 PM
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
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.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
So now i am learning about the keyword static. I read this on php.net and set about testing some of these statments.
i make a simple class
class Foo {
public static function aStaticMethod() {
// ...
echo "hello";
}
}
Foo::aStaticMethod();
I then take away the static keyword from the code
error_reporting(E_ALL);
class Foo {
public function aStaticMethod() {
// ...
echo "hello";
}
}
Foo::aStaticMethod();
and i get exactly the same result. So it seems i can call a non static method statically. Why is this? Have i misunderstood this statement?
Then i set about testing another statement
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.
class Foo {
static public $property = "hello";
public static function aStaticMethod() {
// ...
echo $this->$property;
}
}
Foo::aStaticMethod();
i get
PHP Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/zend/static2.php on line 8
but i can use
class Foo {
static public $property = "hello";
public static function aStaticMethod() {
// ...
echo self::$property;
}
}
Foo::aStaticMethod();
This works. I'm getting somewhere. But why do i have to use the scope resolution operator :: why can't i use the $this-> in here. I thought the whole point was to allow access to a member without having to create an instantiation. So giving you access from outside as with Foo::aStaticMethod() but why does it matter what you use inside the class.
Im really at a loss here at understanding this whole static concept any help would be greatly appreciated. It seems to me you might as well just use a procedural function instead of a static function.
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
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.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
So now i am learning about the keyword static. I read this on php.net and set about testing some of these statments.
i make a simple class
class Foo {
public static function aStaticMethod() {
// ...
echo "hello";
}
}
Foo::aStaticMethod();
I then take away the static keyword from the code
error_reporting(E_ALL);
class Foo {
public function aStaticMethod() {
// ...
echo "hello";
}
}
Foo::aStaticMethod();
and i get exactly the same result. So it seems i can call a non static method statically. Why is this? Have i misunderstood this statement?
Then i set about testing another statement
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.
class Foo {
static public $property = "hello";
public static function aStaticMethod() {
// ...
echo $this->$property;
}
}
Foo::aStaticMethod();
i get
PHP Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/zend/static2.php on line 8
but i can use
class Foo {
static public $property = "hello";
public static function aStaticMethod() {
// ...
echo self::$property;
}
}
Foo::aStaticMethod();
This works. I'm getting somewhere. But why do i have to use the scope resolution operator :: why can't i use the $this-> in here. I thought the whole point was to allow access to a member without having to create an instantiation. So giving you access from outside as with Foo::aStaticMethod() but why does it matter what you use inside the class.
Im really at a loss here at understanding this whole static concept any help would be greatly appreciated. It seems to me you might as well just use a procedural function instead of a static function.