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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:54:26+00:00 2026-06-15T04:54:26+00:00

I need to get all declared classes which are have extended another parent class.

  • 0

I need to get all declared classes which are have extended another parent class.

So for example…

class ParentClass {

}

class ChildOne extends ParentClass {

}

class ChildTwo extends ParentClass {

}

class ChildThree {

}

I need an array that outputs this:

array('ChildOne', 'ChildTwo')

I’m new to PHP OOP, but based on some Googling, I came up with this solution.

$classes = array();

foreach( get_declared_classes() as $class ) {
    if ( is_subclass_of($class, 'ParentClass') ){
        array_push($classes, $class);
    }
}

What I want to ask is whether this is the best practice to do what I want to do, or is there a better way? The global scope will contain a lot of other classes that isn’t a child of ParentClass. Is looping through all declared classes the best way to go?

EDIT (clarification of purpose):

What I want to achieve with this is to instantiate each child class extending the parent class.

I want to do $childone = new ChildOne; $childtwo = new ChildTwo; for every child of ParentClass.

  • 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-15T04:54:27+00:00Added an answer on June 15, 2026 at 4:54 am

    Your solution seems fine, though I’m not sure why you’d do this. There is no easy way in php to say, ‘give me all the declared classes of a certain parent class globally’ without actually checking globally each declared class. Even if you have a couple hundred classes loaded to loop through, it shouldn’t be too heavy as they’re all in memory.

    If you’re trying to just track loaded child classes for a specific parent, why not create a registry that tracks them when they’re loaded? You could do this tracking in an autoloader or factory used for the child classes or event as a hack, just by putting something at the top of the class file before the class definition.

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

Sidebar

Related Questions

I need get all items these have no categories int? categoryId = null; var
i have 3 classes: class PassengerDetails which is inherited by class FlightDetails which in
Hello I need to get only the methods declared in a class, and not
I have some ASP.NET web services which all share a common helper class they
I need to get all the appointments from my table for a particular patient,
I need to get all the objects whose id matches a specific pattern .
I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all
I need to get all substrings from string. For ex: StringParser.GetSubstrings([start]aaaaaa[end] wwwww [start]cccccc[end], [start],
I need to get all characters between '(' and ')' chars. var str =
In C# i need to get all values of a particular property from an

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.