أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.
when a regular member method can be accessed directly using its class name i.e without instantiating the class... plz answer in the context of php only.
To access class method or variable without making instance of this class .
The use of declaring a member method as static depends on the situtation and functionality you are about to implement. We declare a method or a variable as static when they don't conceptually belong to an object. Suppose you are building an inventory application, you will create many classes for each functionality, say stock, invoice etc. Suppose you also need many other different operations like finding sum, difference, printing string etc. Now you declare these functions as static inside a class because each of these functions do different things. Lets see an example:
class MyUtil{
public static function printString($str){
echo $str;
}
public static function getSum($a,$b){
return $a+$b;
}
}
MyUtil::printString('Hellooo');
MyUtil::getSum(3,4);
See these statements look more meaningfull, also these functions are not conceptually or logically related in any manner. So we just clubbed together a set of handfull functions.
Hope you understand my point.
Hi,
You can simply define/use a static method only in case : you are'nt using an object of the same class in that method.