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

  • Home
  • SEARCH
  • 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 591397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:37:49+00:00 2026-05-13T15:37:49+00:00

Ok my problem is as follows; I have a class that describes a pet

  • 0

Ok my problem is as follows;

I have a class that describes a pet with this constructor;

public function __construct($name, $type, $age)

So what I want to do is make a number of pet objects, then I want to print all the attributes of all the objects of this class so that it looks something like this

What is the best way of going about it? I know how to iterate through an object’s variables, but my main concern is how to iterate through all objects of a certain class. I would love it if someone could show me a code example of something, particularly if there is a way to do it without the use of arrays.

Any help is appreciated!

  • 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-13T15:37:50+00:00Added an answer on May 13, 2026 at 3:37 pm

    Normally you would expect to have some way of tracking the instances you’ve created, maybe in an array or some kind of containing class.

    But for the sake of argument, you could check all the variables in the current scope with get_defined_vars(), recursively searching any arrays or objects you find, with something like this:

    function findInstancesOf($classname, $vars)
    {
        foreach($vars as $name=>$var)
        {
            if (is_a($var, classname)) 
            {
                 //dump it here
                 echo "$name is a $classname<br>";
    
            }
            elseif(is_array($var))
            {
                 //recursively search array     
                 findInstancesOf($classname, $var);
            }
            elseif(is_object($var))
            {
                 //recursively search object members
                 $members=get_object_var($var);     
                 findInstancesOf($classname, $members);
            }
        }
    }
    
    $vars = get_defined_vars();
    findInstancesOf('MyPetClass', $vars);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a class with a method as follows: public class SomeClass {
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
Problem 2b goes as follows: 2b. For each subject show the first year that
Our situation is as follows, but I'm curious about this problem in any situation.
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have a class that manages the creation of RTF documents and a method
the problem is as follows: I have got a rendered bitmap in a byte
I have the following code: def f(): class XYZ: # ... cls = type('XXX',
I have a class that fires events delegating to DispatcherObject targets which is marshalling
The problem I have is that I open an Image from a GUI using

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.