is there any performance difference ? whats the main difference anyway ? and there is more like
public static function
static public function
is it the same ?
sorry for a newbie question thanks for looking in
Adam Ramadhan
is there any performance difference ? whats the main difference anyway ? and there is more like
public static function
static public function
is it the same ?
sorry for a newbie question thanks for looking in
Adam Ramadhan
There is no difference in the two :
public static function
static public function
Both a accessible outside of the class. In both, you can't use $this inside the function because a static function does not requiere an object to be instanciated.
Consider you have a Cars class.
// Constructors call to a private static function add_this_car();
$car1 = new Cars('bentley');
$car2 = new Cars('Porsche GT1');
// public static function get_created_cars();
$all_cars = Cars::get_created_cars();
This way a class can take care of all instances of it there is (and a reference to each). And things that should be handle by the class itself and do not requiere an instance to be created.
Think of a class full of static methods like a nifty container to hold related functions together.
Say you want to create a class full of your favorite string formatting functions.
Then you can say MyClass::MakeUppercase($string)
to make the string upper case. You can say MyClass::MakeLowercase($string)
to make the string lower case.
Since your utility class doesn't represent any kind of object, and doesn't have any kind of internal state to keep track of, you have no reason to instantiate objects of it. No $myvar = new MyClass()
before you can use the functions. That'd just waste memory creating objects that never have different internal states to keep track of.