Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:08:14+00:00 2026-05-16T03:08:14+00:00

I’m wanting to detect all objects(classes) defined in a php file (example: flavors.php). The

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:08:14+00:00Added an answer on May 16, 2026 at 3:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
In my XML file chapters tag has more chapter tag.i need to display chapters
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.