PHP:如何循环遍历动态数量的类

发布于 2024-09-12 14:02:52 字数 1155 浏览 7 评论 0原文

我想检测 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2024-09-19 14:02:53

您可以使用 Zend_Reflection包,它扩展了常规反射 API

$r = new Zend_Reflection_File('flavors.php');
$classes = $r->getClasses();
echo "File holds " . count($classes) . ":\n";
foreach ($classes as $class) {
    echo $class->getName() . "\n";
}

由于 Zend Framework 具有随意使用的架构,因此您不必使用任何其他类。您不必将应用程序迁移到 Zend Framework,并且可以独立使用 Zend_Reflection

另外,您可能想为您的类提供一个界面

interface IFlavor
{
    public function getName();
    public function getPrice();
    public function getColor();
}

这样,您可以反映一个类是否是 Flavor 类型,例如,

$class->implementsInterface('IFlavor')

确保文件中的类确实是 Flavors。这也将使您的代码更易于维护,因为它将确保您的风格都具有相同的基本方法,并且您可以使用接口作为 TypeHint通常是编码的好习惯

但是,如果风味类仅包含这两个属性并且没有其他责任,您可能希望将它们制作为单个风味类,而不是拥有多个不同的类,例如

class Flavor implements IFlavor
{
    protected $_name;
    protected $_color;
    protected $_price;

    public function __construct($name, $color, $price)
    {
        $this->_name  = $name;
        $this->_color = $color;
        $this->_price = $price;
    }

    // getter methods
}

而不是使用反射来查找类文件中,您可以简单地使用配置文件来存储可用的口味。有一个知道如何加载配置文件并根据需要为其提供延迟初始化风味的方法的类。由于 Flavors 实际上是值对象(在 DDD 意义上),因此您不需要多个 Flavor 实例具有相同的名称,因此管理类还可以保存任何先前创建的实例并在请求时返回这些实例。

You can use the Zend_Reflection package, which extends the regular Reflection API.

$r = new Zend_Reflection_File('flavors.php');
$classes = $r->getClasses();
echo "File holds " . count($classes) . ":\n";
foreach ($classes as $class) {
    echo $class->getName() . "\n";
}

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:

interface IFlavor
{
    public function getName();
    public function getPrice();
    public function getColor();
}

This way, you can reflect whether a class is a Flavor type, e.g.

$class->implementsInterface('IFlavor')

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.

class Flavor implements IFlavor
{
    protected $_name;
    protected $_color;
    protected $_price;

    public function __construct($name, $color, $price)
    {
        $this->_name  = $name;
        $this->_color = $color;
        $this->_price = $price;
    }

    // getter methods
}

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.

送你一个梦 2024-09-19 14:02:53

您应该查看 token_get_all()

You should have a look at token_get_all()

北城半夏 2024-09-19 14:02:53

您需要将口味列表存储在数组中。一旦它们进入数组,您就可以循环遍历它们并进行排序。处理它们

$flavours = array('vanilla', 'chocolate');

foreach($flavours as $flavour) {
    echo description_generator($flavour);
}

You need to store the list of flavours in an array. Once they're in an array you can loop through them & process them

$flavours = array('vanilla', 'chocolate');

foreach($flavours as $flavour) {
    echo description_generator($flavour);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文