PHP:如何循环遍历动态数量的类
我想检测 php 文件中定义的所有对象(类)(例如:flavors.php)。对象的数量将动态定义,因此我需要在运行时使用函数处理可用的对象,直到对象耗尽。对象耗尽后,程序停止。
我对代码有完全的控制权,所以如果有更好的方法来存储变量,并仅使用 php 来组织它们,请告诉我。
作为学习经验,我试图让 php 操作一组对象,而不知道预先存在的对象的数量或名称。
这是我试图编码的逻辑:
while(THERE ARE STILL CLASSES TO PROCESS IN FLAVORS.php)
{
$var = description_generator(CLASS_NAME SENT TO THE FUNCTION);
print($var);
}
对于上下文,这是整个程序:
flavors.php
class vanilla
{
// property declaration
public $color = 'white';
public $price = '1.25';
}
class chocolate
{
// property declaration
public $color = 'brown';
public $price = '1.50';
}
main.php
{
function description_generator($class_name)
{
$selected_flavor_class = new $class_name();
$flavor_color = $selected_flavor_class->flavor_color;
$flavor_price = $selected_flavor_class->flavor_price;
return "$flavor_color"."<br />"."$flavor_price";
}
while(THERE ARE STILL CLASSES TO PROCESS)
{
print($var = description_generator(CLASS_NAME));
}
总而言之,有没有办法让 PHP 处理不确定数量的类?还有一种更好的方法来创建和组织存储多个变量的对象,例如 Chocolate = 'brown', '1.50' 而不仅仅是使用简单的数组?
I'm wanting to detect all objects(classes) defined in a php file (example: flavors.php). The number of objects will be defined dynamically, so I need to process the available objects with a function at runtime until the objects are exhausted. After the objects are exhausted the program stops.
I have full control over the code, so if there is a better way to store the variables, and keep them organized using only php, please let me know.
As a learning experience, I'm trying to make php manipulate a set of objects without knowing the number, or the names of the objects that exist in advance.
This is the logic I'm trying to code:
while(THERE ARE STILL CLASSES TO PROCESS IN FLAVORS.php)
{
$var = description_generator(CLASS_NAME SENT TO THE FUNCTION);
print($var);
}
For context this is the entire program:
flavors.php
class vanilla
{
// property declaration
public $color = 'white';
public $price = '1.25';
}
class chocolate
{
// property declaration
public $color = 'brown';
public $price = '1.50';
}
main.php
{
function description_generator($class_name)
{
$selected_flavor_class = new $class_name();
$flavor_color = $selected_flavor_class->flavor_color;
$flavor_price = $selected_flavor_class->flavor_price;
return "$flavor_color"."<br />"."$flavor_price";
}
while(THERE ARE STILL CLASSES TO PROCESS)
{
print($var = description_generator(CLASS_NAME));
}
To summarize, is there a way to make PHP process through an undetermined number of classes? Also is there a better way to create and organize objects that store multiple variables such as chocolate = 'brown', '1.50' without just using a simple array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Zend_Reflection
包,它扩展了常规反射 API。由于 Zend Framework 具有随意使用的架构,因此您不必使用任何其他类。您不必将应用程序迁移到 Zend Framework,并且可以独立使用
Zend_Reflection
。另外,您可能想为您的类提供一个界面:
这样,您可以反映一个类是否是 Flavor 类型,例如,
确保文件中的类确实是 Flavors。这也将使您的代码更易于维护,因为它将确保您的风格都具有相同的基本方法,并且您可以使用接口作为 TypeHint 和 通常是编码的好习惯
但是,如果风味类仅包含这两个属性并且没有其他责任,您可能希望将它们制作为单个风味类,而不是拥有多个不同的类,例如
而不是使用反射来查找类文件中,您可以简单地使用配置文件来存储可用的口味。有一个知道如何加载配置文件并根据需要为其提供延迟初始化风味的方法的类。由于 Flavors 实际上是值对象(在 DDD 意义上),因此您不需要多个 Flavor 实例具有相同的名称,因此管理类还可以保存任何先前创建的实例并在请求时返回这些实例。
You can use the
Zend_Reflection
package, which extends the regular Reflection API.Since Zend Framework has a use-at-will architecture, you do not have to use any of the other classes. You do not have to migrate your application to Zend Framework and can use
Zend_Reflection
standalone.Also, you might want to give your classes an interface:
This way, you can reflect whether a class is a Flavor type, e.g.
to make sure the classes in the file really are Flavors. This will also make your code more maintainable because it will ensure your flavors all have the same basic methods and you can use the interface as TypeHint and it is generally good practise to code against an interface.
However, if the flavor classes only hold these two properties and have no other responsibility, you might want to make them into a single Flavor class, instead of having multiple different classes e.g.
Instead of using Reflection to find classes in your flavors file, you could simply use a Config file to store available flavors. Have a class that knows how to load the config file and give it a method to lazy init Flavors as needed. Since the Flavors are effectively Value Objects (in the DDD sense), you dont need more than one Flavor instance of the same name, so the managing class could also hold any previously created instances and return those when requested.
您应该查看
token_get_all()
You should have a look at
token_get_all()
您需要将口味列表存储在数组中。一旦它们进入数组,您就可以循环遍历它们并进行排序。处理它们
You need to store the list of flavours in an array. Once they're in an array you can loop through them & process them