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?