0

I have two methods in a class, one of which is static. I want to access the non-static method from within the static method. Is that possible? I tried this:

class Foo {
   public function qux(){

   }
   public static function waldo(){
       self::qux(); // Non-static method Foo::qux() should not be called statically
   }
}

Is making qux a static method the only way to achieve this? What if the user doesn't want qux() to be a static method?

1

2 Answers 2

1

This should work as you need:

class Foo {
   public function qux(){

   }

   public static function waldo(){
       $foo = new Foo();
       $foo->qux();
   }
}

There is no other way to call a dynamic method/function without creating the object itself first.

Of course, if you will use the object only one-time and call all methods or functions immediately, you could use something like this:

class Foo {
   public function qux(){

   }

   public static function waldo(){
       (new Foo())->qux();
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0
class Foo {

   public function qux(){

   }

   public static function waldo(){
       $obj = new Static();
       $obj->qux();

   }
}

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.