Call static method from instance in PHP, future deprecation?
Asked Answered
R

2

29

While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example:

class MyExample{
    private static $_data = array();
    public static function setData($key, $value){
        self::$_data[$key] = $value;
    }
    // other non-static methods, using self::$_data
}

// to decouple, another class or something has been passed an instance of MyExample
// rather than calling MyExample::setData() explicitly
// however, this data is now accessible by other instances
$example->setData('some', 'data');

Are there plans to deprecate this sort of functionality, or am I right to expect support for this going forward? I work with error_reporting(-1) to ensure a very strict development environment, and there aren't any issues as of yet (PHP 5.3.6) however I am aware of the reverse becoming unsupported; that is, instance methods being called statically.

Remedial answered 18/6, 2011 at 20:20 Comment(2)
$example::method() will look for a class with the name $example evaluates to, so I don't believe that will work (barring trickery with __toString()) ... or not? I didn't expect that to work, however it appears to have. I'm curious though, syntactically (and technically) if that's the best direction.Remedial
well i hope its not going to be deprecated anytime soon... its going to break a lot of code, ha!Cardiograph
H
42

From the Php documentation:

A property declared as static can not be accessed with an instantiated class object (though a static method can).

So I think it will be forward-supported for a long time.

Hereof answered 2/11, 2011 at 16:30 Comment(2)
As of 2016, "For the record, there are currently no plans, neither specific nor speculative, to deprecate this functionality."Charitacharitable
@Charitacharitable What about 2021?Roosevelt
D
6

You can always use:

$class = get_class($example);
$class::setData('some', 'data');

If you want to be explicit about the method being static.

Or, from inside the class you can use the keywords self and static (and the function get_called_class) also in non static methods:

self::setData('some', 'data');

or

static::setData('some', 'data');

or

$class = get_called_class();
$class::setData('some', 'data');
  • self references the class where the method is declared: if the method is in class Animal and Parrot extends Animal, self will reference class Animal even if called in an object of type Parrot.

  • static references the class of the object being used (the class of $this), so if the method is in Animal and it is called on an instance of Parrot it references class Parrot (like the get_class and get_called_class functions)

So, if you are in object Parrot that extends Animal, and the following code is written in class Animal:

self::setData('some', 'data');

is like

Animal::setData('some', 'data');

and

static::setData('some', 'data');

is like

Parrot::setData('some', 'data');

As a suggestion I would rather make a non static method with an informative name that calls the static one, rather then get the class from outside every time. It seems to me more concise, clean and clear:

public function setStaticData($a,$b) {
    return static::setData($a,$b);
}
Dewittdewlap answered 16/3, 2022 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.