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 7915891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:36:44+00:00 2026-06-03T14:36:44+00:00

I am working on a PHP project in which a lot of classes are

  • 0

I am working on a PHP project in which a lot of classes are high leveled in the class tree, i.e. they have a lot of parent classes.

At some level, consider there is a class Food, which has quite a lot of subclasses and sub-subclasses etc. I want to implement a method getFoodClasses() in class Food that gives me an array of all subclasses that lead to the current instance, i.e. classes in the tree between class Food and the current instance, including the latter. The Food class itself and all its superclasses should not be included in the result.

Example: if a subclass of Food would be Vegetable, which has a subclass Fruit, which then has a subclass Banana, then the result of on (Banana) $b->getFoodClasses(), needs to result in `array(‘Vegetable’, ‘Fruit’, ‘Banana’).

So,

class Food extends SomeBaseClass
{
  public function getFoodClasses()
  {
    /* Here goes the magic! */
  }

}

class Vegetable extends Food      {}
class Fruit     extends Vegetable {}
class Banana    extends Fruit     {}

$b = new Banana;
print_r($b->getFoodClasses());

Which results in:

Array
(
    [0] => Vegetable
    [1] => Fruit
    [2] => Banana
)
  • 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-06-03T14:36:46+00:00Added an answer on June 3, 2026 at 2:36 pm

    I ended up with this function.

    /**
     * Calculates the widget path between this class and the current instance.
     * @return array An array with all subclasses in the path between this class and the current instance.
     */
    public function getFoodClasses()
    {
       $reflClass = new ReflectionClass(get_class($this));
       $classes   = array();
    
       while ($reflClass->getName() != __CLASS__)
       {
          $classes[] = $reflClass->getName();
          $reflClass = $reflClass->getParentClass();
       }
    
       return $classes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project with PHP. I have a php page which
Ok, I'm working on a php college project, and I have to let students
My team working on a php/MySQL website for a school project. I have a
I am currently working on a PHP project which includes extensive database usage. The
I am developing a PHP project and using PDT with XDebug which is working
I am currently working on a php/javascript project which retrieves information from a database
I'm working on a project which needs to be Unicode aware. PHP provides bunch
On a current project which I'm currently working, i have a symfony task that
I'm working on a new php project with CodeIgniter and I have few questions
I am working on a project which has a lot of JavaScript. I think

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.