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");
6
  • Please, don't use static methods. Commented Nov 24, 2013 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. Commented Nov 25, 2013 at 7:44
  • The problem with static methods and variables is that they are global. Commented Nov 25, 2013 at 8: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 stackoverflow.com/a/4690508/1292556 Commented Nov 29, 2013 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. Commented Nov 29, 2013 at 11:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.