1

I wrote a small test scenario for testing mixed use of static and non-static methods. When calling a public method as a static method it generates a strict warning. That got me thinking that perhaps calling a static method as a public method would generate a similar warning, but it did not.

Is it ok to call static methods as a non-static method? Are there any consequences to doing so?

<?php
class TestS {
  static function _testS() {
    echo("Hello Static<br>\n");
    // Strict warning: Non-static method TestS::_tS() should not be
    // called statically in TestS::_tS()
    self::_tS();
  }
  public function _tS() {
    echo("tS<br>\n");
  }
  public function _testP() {
    echo("Hello Public<br>\n");
    self::_tP();
  }
  static function _tP() {
    echo("tP<br>\n");
  }
  public function _testSP() {
    $this->_tP();
  }
}

$test = new TestS();
echo("///////////<br>\n");
$test->_testP();
echo("///////////<br>\n");
TestS::_testS();
echo("///////////<br>\n");
$test->_testSP();
echo("///////////<br>\n");
forsvunnet
  • 1,238
  • 10
  • 17
  • Please, don't use static methods. – tereško Nov 24 '13 at 21:54
  • Are there any compelling arguments to not to use static methods other than preference? I like to use static methods/classes to keep the global scope clean of lots of small utility functions. – forsvunnet Nov 25 '13 at 07:44
  • The problem with static methods and variables **is** that they are global. – tereško Nov 25 '13 at 08:08
  • In most of the cases where I use static methods the goal is to make those methods global. I don't see why that is a problem? Using static methods over functions serves to keep the global scope cleaner and gives the programmer a tool to organize functions in a logical manner (group methods by class). E.g: Math::sin(); Stat::std(); Colour::generate('red'); $objects = ObjectBuilder::build_list($items); See http://stackoverflow.com/a/4690508/1292556 – forsvunnet Nov 29 '13 at 11:01
  • It also bind all your code to specific class names. That is know as "tight coupling". Essentially what you are writing is procedural code, that has nothing to do with object-oriented programming. For you, the class is nothing but a glorified namespace. – tereško Nov 29 '13 at 11:17
  • Correct. I use PHP mainly as an interface to tweak wordpress or drupal websites, not for "programming" per se. My prefered poison for such things is javascript. It does not mean however that I'm not drawn to the power of OOP when working with WP or drupal. I will usually only make classes and objects when a feature requires extensive amount of custom code, but even so it still requires quite a lot of procedural code to integrate these with the CMS. In that case I prefer making an utility class over several utility functions. Do you have an alternative method to suggest? – forsvunnet Nov 29 '13 at 11:42

1 Answers1

1

Is it ok to call static methods as a public method?

Theoretically, it is. That's because a static method doesn't reference $this anywhere, so it will work when called either statically or as an instance method.

Are there any consequences to doing so?

It's a good practice to indicate explicitly what kind of method you're calling; when looking back on your code you will know exactly which method is static because of the way you're calling it, with the notable exception of using parent::method() inside an instance method.

It's also a good practice to avoid using static methods in the first place; if you have a lot of them, it's often indicates a code smell and should be refactored.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309