65

I am working on PHP code.

Here is the sample code to explain my problem:

class Foo {

    public function fun1() {
             echo 'non-static';   
    }
    public static function fun2() {
        echo "static" ;
        //self::fun1();
        //Foo::fun1(); 
    }
}

How can I call the non-static method from the static method ?

Note: Both functions are used throughout the site, which is not known. I can't make any changes in the static/non-static nature of them.

6
  • I think non-static methods can be called by object only. because its dependent on class and static methods are independent. Commented Jan 13, 2017 at 9:49
  • Yeah, but inside class, how ? Shall I create object of same class in same own class's function ? Commented Jan 13, 2017 at 9:50
  • check this : stackoverflow.com/a/2396427/2815635 Commented Jan 13, 2017 at 9:54
  • what is the problem:-eval.in/715502 Commented Jan 13, 2017 at 9:54
  • @Anant Non-static method getCommonData() should not be called statically. This message was showing for self:: and Foo:: Commented Jan 13, 2017 at 9:56

2 Answers 2

139

You must create a new object inside the static method to access non-static methods inside that class:

class Foo {

    public function fun1()
    {
        return 'non-static';
    }

    public static function fun2()
    {
        return (new self)->fun1();
    }
}

echo Foo::fun2();

The result would be non-static

Later edit: As seen an interest in passing variables to the constructor I will post an updated version of the class:

class Foo {

    private $foo;
    private $bar;

    public function __construct($foo, $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function fun1()
    {
        return $this->foo . ' - ' . $this->bar;
    }

    public static function fun2($foo, $bar)
    {
        return (new self($foo, $bar))->fun1();
    }
}

echo Foo::fun2('foo', 'bar');

The result would be foo - bar

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

3 Comments

How do you pass variables to be used by the constructor?
I would rather keep the class with static function as clean as possible. I would treat it as a static class. And in a static class you would not have a constructor would you? in PHP documentation is states: Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. It would not surprise me if the PHP developers will remove the possibility to create non-static functions in a static class, and perhaps they also add static in front of class in future version. Just my guess. I have no knowledge of php development.
@Trond There is valid usecase where class with static members / properties should instantiate singleton using constructor and static function should call non-static one. I am implementing Swoole HTTP Request event providing it a call-back as a Class with static member. Static Member does two things; it creates Singleton Object of the class by doing initialization in class constructor, and second this static members does is to call a non-static method 'run()' to handle Request (by bridging with Phalcon). Hence, static class without constructor and non-static call will not work for me.
5

The main difference would be that you can call static methods for a class without having to instantiate an object of that class. So, in your static method try

Foo $objInst = new Foo();
$objInst->fun1();

But I don't see how this would make any sense in any context.

1 Comment

Thanks ! Working fine. But is there any rule whether we should instantiate object of same class in same class or not ?

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.