大佬教程收集整理的这篇文章主要介绍了我需要收集一个类数组以在php中调用其静态变量,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我要说的第一件事是,这不是一个容易解释的问题,因此,如果它看起来令人困惑,请耐心等待.
我有一组这样的课程
class Product {
public static $static_type = 'product';
public static $static_table = 'product_table';
public function __construct($params) { //do some }
}
然后是新闻,事件等类别
从另一个类中,我需要以迭代方式访问这些类中的那些静态变量.就像是:
//...
if (Product::$static_typE) { //do some }
else if (News::$static_typE) { //do other }
//...
我想以一种周期的方式进行转换,例如以这种方式进行foreach(这是不正确的,但对我的问题是有意义的)
foreach ($classes as $class) {
echo $class::$static_type; //brrrr, just to render the idea :)
}
因此,我想到了具有静态方法的单例/静态类,该方法返回我的类的数组(未实例化).像这样:
class Conf {
public function __construct() {
//nothing
}
public static function get_class_array () {
//how to do this???
}
}
接着
foreach (Conf::get_class_array() as $class) {
echo $class::$static_type; //brrrr, just to render the idea :)
}
我怎么能做到这一点?在这种情况下,我不想实例化产品,新闻或其他产品.
编辑:评估是邪恶的,我不想使用它. get_declared_class没有技巧,如果没有办法解决,我将使用反射,我认为这是所提到的:(.
编辑:同时,我将以这种方式执行Conf :: get_class_array()
public static function get_class_array () {
return array(new ReflectionClass('Prodotto'), new ReflectionClass('News'));
}
foreach (Conf::get_class_array() as $class) {
echo $class->getStaticPropertyValue('static_type');
}
@H_675_57@解决方法:我认为您无法做到这一点.但是,您可以执行以下操作之一:
$properties = get_class_vars('Product');
echo $properties['static_type'];
要么
$class = new ReflectionClass('product');
echo $class->getStaticPropertyValue('static_type');
请注意,在PHP 5.3中,echo $class :: $static_type;会工作(http://php.net/manual/en/language.oop5.static.php)
以上是大佬教程为你收集整理的我需要收集一个类数组以在php中调用其静态变量全部内容,希望文章能够帮你解决我需要收集一个类数组以在php中调用其静态变量所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。