Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Calling self on a non-static method

Why is the 'self'-call to a non-satic method in this example working?

class A{

    protected function aNonStaticMethod(){
        return __class__;
    }

    public function aEcho(){
        echo self::aNonStaticMethod();
    }
}

Thanks for explanation.

like image 561
user2853437 Avatar asked Oct 07 '13 06:10

user2853437


People also ask

Can I call non-static method from static method PHP?

In PHP 5, calling non-static methods statically generates an E_STRICT warning. In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. See Static methods (php.net) for details. In the following example, the method foo() is called as dynamic while actually it is static.

Can we call non-static method?

A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

Can you call a non-static method without an instance?

In a non-static method, the method use runtime or dynamic binding. So that we cannot access a non-static method without creating an instance.

How do you call a non-static main method?

When you need to use it, you don't need to create a new Integer object, you simply call it. The same thing for main(). If you need to call a non-static member from it, simply put your main code in a class and then from main create a new object of your newly created class. Save this answer.


2 Answers

In your simple example $this and self is interchangable. But be aware of the different method resolving when dealing with inheritance (i added static for completeness):

class A {
    protected function aNonStaticMethod(){
        return __class__;
    }

    public function selfEcho(){
        echo self::aNonStaticMethod();
    }

    public function staticEcho(){
        echo static::aNonStaticMethod();
    }

    public function thisEcho(){
        echo $this->aNonStaticMethod();
    }
}

class B extends A {
    protected function aNonStaticMethod(){
        return __class__;
    }
}

$b = new B();
$b->selfEcho(); // A
$b->staticEcho(); // B
$b->thisEcho(); // B
like image 174
warly Avatar answered Sep 21 '22 18:09

warly


Calling non-static method statically

Theoretically it should not work, but as this comment says:

There was no static keyword in php4 but php4 did allow for static calls. To maintain backwards compatibility this was left in when the static keyword was added in php5.

This comment is supported by this official php.net wiki:

This is already deprecated if the call occurs from an instance method. Not annotating methods as static is an obsolete PHP4-ism.

You really should not call non-static method statically - it does not make sense (if there is a static keyword).

Avoid calling non-static methods statically completely!

...because a) it is a bad approach and b) the PHP docs say:

Caution
In PHP 5, calling non-static methods statically generates an E_STRICT level warning.

AND

Warning
In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.

Using :: operator for non-static calls - may be a good approach!

As @Kontrollfreak pointed out and as this docs say the :: operator is not limited to static calls:

the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class

So it is OK if you reference this way a method or properties from a parent class - which is not limited to a direct parent.

EDIT: do not mistake this for Fascade etc. software patterns!

During writing this answer I forgot to mention that there might be cases, when the call is static, but internally it is calling dynamic method - for more info see patterns like Facade or Singleton.
However do NOT mistake these with issue described above! (issue above is about using direct static call on dynamic thing that should be called dynamically, these patterns are about calling static methods statically, which then may dynamically invoke something dynamic (internally)).

like image 24
jave.web Avatar answered Sep 20 '22 18:09

jave.web