-1

Please any one tell me difference between static and non static class and method.

 <?php

  class  a
{

public  static function sum($a,$b)
    {
        return $a+$b;

    }

}


$obj = new a();
echo $obj->sum(20,30);
echo $c=a::sum(10,10);

?>
dtanwar
  • 68
  • 9
  • Static methods/properties cannot be accessed through the object using the arrow operator -> , see [mannual](http://php.net/manual/en/language.oop5.static.php) – kamal pal Jan 23 '16 at 04:57
  • http://www.codedwell.com/post/59/static-vs-non-static-methods-in-phphttp://php.net/manual/en/language.oop5.static.php – Chaudhary Amar Jan 23 '16 at 05:01
  • 4
    Possible duplicate of [php static function](http://stackoverflow.com/questions/902909/php-static-function) – taglia Jan 23 '16 at 05:02

4 Answers4

0

A static method is the same for every instance of a class. It is attached to the class itself. A non-static (instance) method is attached to an object, an instance of a class. The functional difference is that an instance method has access to instance properties, whereas a static method does not, because it exists at the class level.

Alex Sharp
  • 533
  • 5
  • 12
0

Here is an extract from PHP Manual

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

m453h
  • 114
  • 1
  • 5
0

Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute. In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.

Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
0

My code is not php. It's more of a generic representation. Let's say you want to find sine of a number. You'd mostly likely rely on built in math library by doing something like Math.sine(30). You see how I accessed the method sine inside the Math class without creating an instance of the Math class. These methods are static methods. If sine was not a static method then I'd have to do something like

Math m = new Math(); // create instance
m.sine(30); // call instance method

And for static method

Math.sine(30) // static call
Lordking
  • 1,413
  • 1
  • 13
  • 31