在php中查找调用函数的类名 [英] Find the class name of the calling function in php
本文介绍了在php中查找调用函数的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有:
class Zebra{
public static function action(){
print 'I was called from the '.get_class().' class'; // How do I get water here?
}
}
class Water{
public static function drink(){
Zebra::action();
}
}
Water::drink();
如何从斑马类中获取水"?
How do I get "water" from the zebra class?
(这是用于 php 5.3)
(This is for php 5.3)
推荐答案
一个不太好的解决方案是:使用 __METHOD__
或 __FUNCTION__
或 __CLASS__
.并将其作为参数传递给被调用的函数.http://codepad.org/AVG0Taq7
One not so good solution is :
use __METHOD__
or __FUNCTION__
or __CLASS__
.
and pass it as parameter to function being called.
http://codepad.org/AVG0Taq7
<?php
class Zebra{
public static function action($source){
print 'I was called from the '.$source.' class'; // How do I get water here?
}
}
class Water{
public static function drink(){
Zebra::action(__CLASS__);
}
}
Water::drink();
?>
这篇关于在php中查找调用函数的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文