2

I'm using PHP as my programming language. I really don't understand the use of static method and variables.

  1. When a methold should be static?/ What methods should be static ?
  2. How to identify what methold should be static?
  3. Pros and Cons static method and variables ?

Thanks

j0k
  • 22,600
  • 28
  • 79
  • 90
Techie
  • 44,706
  • 42
  • 157
  • 243
  • 2
    statics are just a fancy way to group procedural code into a class. Except for very special cases, avoid them. – JvdBerg Sep 23 '12 at 12:19
  • I don't why the hell they closed my questions? Looks like not knowing something is a crime :O – Techie Sep 23 '12 at 13:09
  • methods should be static when you trying to mask your procedural code with language tools meant for object oriented programming. – tereško Sep 23 '12 at 16:31

3 Answers3

2

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).

When you are working on a large OOP based project, you’ll no doubt be working with many classes (both parent and child classes). An unfortunate consequence of this is that in order to access elements from different classes, they must manually be passed through each class (or worse, storing an instance in a global variable). This can be painstakingly frustrating and can lead to messy code and overall bad project design. Thankfully, static elements are accessible from any context (i.e. anywhere in your script), so you can access these methods without needing to pass an instance of the class from object to object.

also check this Does static method in PHP have any difference with non-static method?

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
2

A method should be static when it is not bound to instance variables. If it is doing plane processing and taking all the variables from function inputs. It can be marked as static.

Its advantage is you need not to create instances to invoke functionality and hence saves memory.

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
  • why functions without static keyword are possible to access with scope resolution operator ? – Techie Sep 23 '12 at 12:29
-1

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

<?php class Foo {
    public static function aStaticMethod() {
        // ...
    } }

$classname = 'Foo'; $classname::aStaticMethod(); // ( PHP 5.3.0) ?> 

"it can be initiated with out an OBJECT"

cc4re
  • 4,821
  • 3
  • 20
  • 27
  • why do we create objects when we can go on calling function with :: ?(given if we make every function static) – Techie Sep 23 '12 at 12:35
  • 1
    nice question :) when you use static keyword to a method > you couple yourself directly to the class. It's impossible to use polymorphism and inheritance , giving importance to Object is what OOP paradigm is. – cc4re Sep 23 '12 at 13:04
  • `A property declared as static can not be accessed with an instantiated class object` this is flat out wrong – Tofandel Aug 03 '22 at 10:04