1

This has proven somewhat hard to explain, and it's probably very stupid design, but out of curiosity, is there a way to get the name of the class of a static method, that is calling another static method in another class.

Class A calls static method B in class B, that calls static method C in class C. In class C, I need the class name of the immediate method that called static method C, which would be class B.

class A {
   function A () {
       echo B::B();
   }
}

class B {
   function B () {
       return C::C();
   }
}

class C {
   function C () {
       return get_called_class();
   }
}

This returns: A. I would like it to return: B.

Is this even possible?

1
  • 1
    you can send it along with the call, as a parameter Commented Apr 10, 2015 at 20:39

2 Answers 2

1

you could use a stacktrace for that. Have a look at http://php.net/debug_backtrace.

Due to the encapsulation of the classes it is not wanted by design that the method knows whats going on outside. Therefore the only way that comes to my mind is this - rather quirky - way.

And you shouldn't rely on that information as it would break encapsulation. If you need that info pass it as parameter!

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

1 Comment

Yes, you're right. I'll pass it along as a parameter instead. Thanks!
0

Not sure exactly what you're trying to do, but have a look at the PHP page for the Static keyword. You mention a static method, but the code doesn't have static anywhere.

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.