0

we have one class

class A { // I am using class B here public function whatever() { $class_b = new B; $class_b->show_caller(); } } 

and other

class B { public function show_caller() { // show me the caller class (should output "A") } } 

I need to get the caller class name. P.S. Inheritance is not an option!

3
  • 1
    Why do you need the caller? Sounds like a design-flaw. Commented Aug 21, 2016 at 15:09
  • Possible duplicate of Print PHP Call Stack Commented Aug 21, 2016 at 15:10
  • Well, I know the architecture is bad, but project it's pretty big and i haven't time to refactor all of it. I just can't use Inheritance. Commented Aug 21, 2016 at 15:11

2 Answers 2

1

Pass class A as a parameter to B::show_caller() and output it's class name using get_class():

class A { // I am using class B here public function whatever() { $class_b = new B; $class_b->show_caller($this); } } class B { public function show_caller($class) { echo get_class($class); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't mean anything. How is it supposed to be dynamic? How wouldn't solution work for you?
I think he mean that $class_b->show_caller($class_b); will return B, not A. But it's really interesting to find solution without parameters
0

We can't provide the knowledge about of where your method is executing to method's class without parameters. Late static binding will work only with inheritance, so it's not your case. The only approach, I can give you, is to use your IDE ability to find method usage in your project (ctrl+mouse1 in PhpStorm). If you're not using any IDE - it's a time to start.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.