Dan
Dan

Reputation: 11

PHP: calling non-static methods with scope resolution operator

Possible Duplicates:
Calling non static method with "::"
Does static method in PHP have any difference with non-static method?

What is the reason for allowing calling non-static methods using ::, given we don't try to access anything inside the object context with $this? Is it a backward compatibility thing, or is it so for some particular reason? Should I get myself used to avoiding using :: to access non-static methods?

class Foo{    
public function Bar(){
    echo "this works just fine"; 
    }
}

Foo::Bar();

Upvotes: 0

Views: 1407

Answers (1)

matthewdaniel
matthewdaniel

Reputation: 1476

There are several reasons why someone might do this.

  • One is that a function may live in a class and may not depend on the class being instantiated to produce results and you may not be able to instantiate the class or it is a heavy instantiation so you just call the function.
  • It is needed to load a singleton.
  • It is helpful in Factory pattern classes
  • Maybe someone just wants to group related functions together rather than just use a naming convention for all the functions
  • can access methods in an abstract class if needed
  • I'm sure there is much more

http://www.ibm.com/developerworks/library/os-php-designptrns/

Upvotes: -1

Related Questions