14

ok I have this code, that I'm studying

 class scope{

    function printme(){
        return "hello";
    }

    public static function printme(){
        return "hello"; 
    }

 }

$s = new scope();
echo $s->printme(); //non-static call
echo "<br>";
echo scope::printme(); //static call

Now, this is not really the code of my project but these are the things I want to do

  1. I want to create a class the will contain static and non-static functions.
  2. I want a function to be available both on static and non-static calls.

As non-static function has a lot of operations on it, I also need to call it as a static function so that I will not need to instantiate the class. Is this possible? or I really needed to rewrite the function to another function or class?

NOTE: tell me if I'm doing some bad programming already.

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • https://stackoverflow.com/a/11331826/4695280 – Boolean_Type May 06 '19 at 10:36
  • Possible duplicate of [PHP - is it possible to declare a method static and nonstatic](https://stackoverflow.com/questions/11331616/php-is-it-possible-to-declare-a-method-static-and-nonstatic) – Boolean_Type May 06 '19 at 10:37

3 Answers3

35

Here is the rule:

A static method can be used in both static method and non-static method.

A non-static method can only be used in a non-static method.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • But if I call non-static method from static method using self:: it works. why? – RN Kushwaha Oct 21 '16 at 06:38
  • Php allowed you to do that doesn't mean you should do that. self:: works because your non-static method doesn't use $this, if you use $this inside, it should not be working. – xdazz Oct 27 '16 at 06:20
  • @RNKushwaha If you call non-static method from static method, if the not-static method using $this, `Fatal error: Uncaught Error: Using $this when not in object context` will happen, if the non-static method does not using $this, `PHP Deprecated: Non-static method test::foo() should not be called statically` will happen. – xdazz Oct 27 '16 at 06:26
6

If the instance of your class is rarely needed, you can have the static method create an instance, call the non-static method and return the value.

class Scope {
    public function mynonstatic() {
    }

    public static function mystatic() {
        $s = new Scope();
        return $s->mynonstatic();
    }
}

Remember that a static method is really just a global function with reduced scope. They are useful, but are should not be created without good reason.

walrii
  • 3,472
  • 2
  • 28
  • 47
2

As non-static function has a lot of operations on it, I also need to call it as a static function so that I will not need to instantiate the class. Is this possible? or I really needed to rewrite the function to another function or class?

If you need it static, then make it static. If you need it not, then keep it the way it is. It is possible from within non-static function to call static function.

class Foo
{
    public function bar()
    {
        Foo::zex();

        // or self::zex() or even $this->zex();
    }

    public static function zex()
    {
    }
}


$foo    = new Foo;
$foo->bar();

Ant the other way around.

class Foo
{
    public function bar()
    {

    }

    public static function zex()
    {
        $foo    = new Foo;
        $foo->bar();
    }
}

When you should do it or should you do it at all is another question. The most common use of the latter is probably the Singleton pattern.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
Gajus
  • 69,002
  • 70
  • 275
  • 438