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. Recent
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
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...
Class Do {
static public function test() {
return 0;
}
}
use like this :
echo Do::test();
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).
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.
Sometimes, it is useful if we can access methods and properties in the context of a class rather than an object. To do this, you can use static keyword. So we can access static method using class name along with scope resolution .
class User
{
public static $name = 'john';
public static function display()
{
return self::$name;
}
}
echo User::display();
Look here we declared a static method in our User class . So if we declare a static method then we can access there using just class name . No need to create object to access there . Hope you will understand all the procedure .
Note : This is actually the main difference with a normal method : $this is not available in such methods.
read full article from here How to Use Static Properties in PHP ?