Dhairya Lakhera
Dhairya Lakhera

Reputation: 4808

Non-static method calling inside a class is not Deprecated in PHP?

As PHP manual stated

As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context).

To check this statement-

I am calling a Non-static method outside a class then its generating error

class A{
    public function foo(){
        echo "testing<br/>";
    }
}
A::foo();

Deprecated: Non-static method A::foo() should not be called statically

But when I am calling a Non-static method inside a class then its not generating Deprecated error

class A{
    public function foo(){
        echo "testing<br/>";
    }

    public function Display(){
        A::foo();    //calling a Non-static method inside statically
        self::foo(); //calling a Non-static method inside statically
    }
}
$obj=new A();
$obj->Display();

Does it deprecated only when calling statically from outside the class?

Upvotes: 3

Views: 2133

Answers (2)

Niklesh Raut
Niklesh Raut

Reputation: 34924

Using A:: or self:: inside class methods, refers as $this->. From Docs

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.

Upvotes: 1

Arthur Almeida
Arthur Almeida

Reputation: 568

The Scope Resolution operator, or the double colon operator, isn't just used for static calls.

Inside a class the operator can be used to reference the class itself in a non-static way.

This is what you have done for self::foo() and A::foo(); To make a static call inside the class you need to use the static::<methodName> statement

Take a look here: http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

Upvotes: 5

Related Questions