0

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();
1

1 Answer 1

-1

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.