28

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • `$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. – Dan Lugg Jun 18 '11 at 20:27
  • well i hope its not going to be deprecated anytime soon... its going to break a lot of code, ha! – prodigitalson Jun 18 '11 at 20:45

2 Answers2

41

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.

Nicolò Martini
  • 5,182
  • 4
  • 32
  • 38
  • 9
    As of 2016, ["For the record, there are currently no plans, neither specific nor speculative, to deprecate this functionality."](https://www.reddit.com/r/PHP/comments/3yph4q/is_it_bad_practice_to_call_a_static_method_via_an/cyg3fct/) – bishop Apr 18 '17 at 20:03
  • 1
    @bishop What about 2021? – Déjà vu Jul 21 '21 at 05:04
3

You can always use:

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

If you want to be explicit about the method beeing 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 reefernce 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 followiong 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);
}
FrancescoMM
  • 2,845
  • 1
  • 18
  • 29