0

Let's say I have three classes set up as follows:

abstract Class A {

public static function testfunction() 
{
   print_r('Hi');
}

}

Class B extends A {

}

Class C extends A {

}

If I call testfunction through class B or C ie

B::testfunction();

Is there a way of printing out the name of the class that called it? So for example, the output could be

"Hi, this function was called by Class B"

Any advice appreciated.

Thanks.

2 Answers 2

4

Is there a way of printing out the name of the class that called it?

Yes, in php 5.3, but not in earlier versions.

See: Late Static Binding and get_called_class

In general, you can avoid this problem by using object instances, rather than static classes. It is often a better solution.

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

Comments

0

In PHP 5.3 you can use get_called_class() in your static test function. This uses late static binding.

Best wishes, Fabian

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.