donRumatta
donRumatta

Reputation: 842

Static methods in PHP

Why in PHP you can access static method via instance of some class but not only via type name?

UPDATE: I'm .net developer but i work with php developers too. Recently i've found this moment about static methods called from instance and can't understand why it can be usefull.

EXAMPLE:

class Foo
{
    public static Bar()
    {
    }
}

We can accept method like this:

var $foo = new Foo();
$foo.Bar(); // ??????

Upvotes: 27

Views: 78584

Answers (5)

ryh
ryh

Reputation: 19

In PHP 7 it seems to be absolutely necessary for you to be able to do $this->staticFunction(). Because, if this code is written within an abstract class and staticFunction() is also abstract in your abstract class, $this-> and self:: deliver different results!

When executing $this->staticFunction() from a (non-abstract) child of the abstract class, you end up in child::staticFunction(). All is well.

However, executing self::staticFunction() from a (non-abstract) child of the abstract class, you end up in parent::staticFunction(), which is abstract, and thusly throws an exception.

I guess this is just another example of badly designed PHP. Or myself needing more coffee...

Upvotes: 0

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

In PHP

the class is instantiated using the new keyword for example;

$MyClass = new MyClass();

and the static method or properties can be accessed by using either scope resolution operator or object reference operator. For example, if the class MyClass contains the static method Foo() then you can access it by either way.

$MyClass->Foo();

Or

MyClass::Foo()

The only rule is that static methods or properties are out of object context. For example, you cannot use $this inside of a static method.

Upvotes: 40

Web Dev guy
Web Dev guy

Reputation: 41

In PHP, while you're allowed to access the static method by referencing an instance of the class, you don't necessarily need to do so. For example, here is a class with a static function:

class MyClass{
    public static function  MyFunction($param){
        $mynumber=param*2;
        return $mynumber;
}

You can access the static method just by the type name like this, but in this case you have to use the double colon (::), instead of "->".

$result= MyClass::MyFunction(2);

(Please note you can also access the static method via an instance of the class as well using "-->"). For more information: http://php.net/manual/en/language.oop5.static.php

Upvotes: 2

Said Afkir
Said Afkir

Reputation: 171

Class Do {
  static public function test() {
      return 0;
  }
}

use like this :

echo Do::test();

Upvotes: 16

Matthew
Matthew

Reputation: 48284

Why in PHP you can access static method via instance of some class but not only via type name?

Unlike what you are probably used to with .NET, PHP has dynamic types. Consider:

class Foo
{
  static public function staticMethod() { }
}

class Bar
{
  static public function staticMethod() { }
}

function doSomething($obj)
{
  // What type is $obj? We don't care.
  $obj->staticMethod();
}

doSomething(new Foo());
doSomething(new Bar());

So by allowing access to static methods via the object instance, you can more easily call a static function of the same name across different types.

Now I don't know if there is a good reason why accessing the static method via -> is allowed. PHP (5.3?) also supports:

$obj::staticMethod();

which is perhaps less confusing. When using ::, it must be a static function to avoid warnings (unlike ->, which permits either).

Upvotes: 7

Related Questions