This article describe the use of static function and their definition.
After Declare a class or methods as static they can access without needing an instantiation of the class.
Because static methods are callable without an instance of the object created, the pseudo-variable
Simply, static functions function independently of the class where they belong.
Entire difference is, you don't get $this supplied inside the static function. If you try to use
Caution
Warning
Well, okay, one other difference: an
In a nutshell, you don't have the object as
view fork
Source : php.net, stackoverflow
After Declare a class or methods as static they can access without needing an instantiation of the class.
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.Simply, static functions function independently of the class where they belong.
$this
means, this is an object of this class. It does not apply to static functionsEntire difference is, you don't get $this supplied inside the static function. If you try to use
$this
, you'll get a Fatal error: Using $this
when not in object context.Caution
In PHP 5, calling non-static methods statically generates anE_STRICT
level warning.
Warning
In PHP 7, calling non-static methods statically is deprecated, and will
generate an E_DEPRECATED
warning. Support for
calling non-static methods statically may be removed in the future.
class Foo {
public static $static_variable = 'STATIC';
public static function aStaticMethod() {
echo 'Called a static function '. __FUNCTION__.' in class'. __CLASS__;
}
}
class Bar extends Foo {
public static function anotherStatic(){
echo parent::$static_variable;
parent::aStaticMethod();
}
}
Foo::aStaticMethod();
printf("Access static variable - %s",Foo::$static_variable);
Bar::anotherStatic();
Well, okay, one other difference: an
E_STRICT
warning is generated by your first example.In a nutshell, you don't have the object as
$this
in the second case, as the static method is a function/method of the class not the object instance.view fork
Source : php.net, stackoverflow
Comments
Post a Comment