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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:56:59+00:00 2026-06-10T17:56:59+00:00

I have a PHP class that can contain an array of its own type

  • 0

I have a PHP class that can contain an array of its own type this class has also a function called hatch() when I call it for one of the array items it gives me a message you can’t call the function. I call it like that

$this->arrayLikeThis[$i]->hatch();

however I made an outer function to hatch the object but I love to call it that way. I know that I can call any function of the the var_dump() of the class is like that:

object(web\ui\structure\tag)#1 (4) {
  ["name"]=>
  string(4) "meta"
  ["type"]=>
  int(1)
  ["attributes"]=>
  array(2) {
    [0]=>
    object(web\ui\structure\attribute)#2 (2) {
      ["attributeName"]=>
      string(7) "content"
      ["attributeValue"]=>
      string(24) "text/html; charset=utf-8"
    }
    [1]=>
    object(web\ui\structure\attribute)#3 (2) {
      ["attributeName"]=>
      string(10) "http-equiv"
      ["attributeValue"]=>
      string(12) "Content-Type"
    }
  }
  ["childNodes"]=>
  array(0) {
  }
}
  • 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-10T17:57:01+00:00Added an answer on June 10, 2026 at 5:57 pm

    Fluffeh is right, your code should work fine if it’s actually a reference to the class (which I highly doubt that it is). Here’s an example that simulates what your class might look like (with the same functions and in an array).

    <?php
        class egg {
            private $identifier;
            private $eggChildren = array();
            public function __construct($identifier) {
                $this->identifier = $identifier;
            }
            public function createEgg($identifier) {
                $this->eggChildren[$identifier] = new egg($this->identifier . " - " . $identifier);
            }
            public function hatch() {
                echo "Just hatched egg with ID " . $this->identifier . "<br />";
            }
            public function hatchAllChildren() {
                foreach ($this->eggChildren as $childID => $eggChild) {
                    $eggChild->hatch();
                }
            }
        }
        class eggs {
            private $arrayLikeThis;
            const COUNT_EGGS = 3;
            const COUNT_EGG_CHILDREN = 3;
            public function createEggs() {
                $this->arrayLikeThis = array();
                for ($i = 0; $i < self::COUNT_EGGS; $i++) {
                    $this->arrayLikeThis[$i] = new egg($i);
                    for ($j = 0; $j < self::COUNT_EGG_CHILDREN; $j++) { 
                        $this->arrayLikeThis[$i]->createEgg($j);
                    }
                }
            }
            public function hatchEggs() {
                for ($i = 0; $i < self::COUNT_EGGS; $i++) {
                    $this->arrayLikeThis[$i]->hatchAllChildren();
                    $this->arrayLikeThis[$i]->hatch();
                }
            }
        }
    
        $eggController = new eggs();
        $eggController->createEggs();
        $eggController->hatchEggs();
    ?>
    

    And this will output

    Just hatched egg with ID 0 - 0
    Just hatched egg with ID 0 - 1
    Just hatched egg with ID 0 - 2
    Just hatched egg with ID 0
    Just hatched egg with ID 1 - 0
    Just hatched egg with ID 1 - 1
    Just hatched egg with ID 1 - 2
    Just hatched egg with ID 1
    Just hatched egg with ID 2 - 0
    Just hatched egg with ID 2 - 1
    Just hatched egg with ID 2 - 2
    Just hatched egg with ID 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three files: one called sql.php witch has a class db that I
I have a PHP class that stores a complex multidimensional array, and rather than
In PHP, is it possible to have a function within a class that's non-static,
I have a custom MVC PHP framework that has a router class, which calls
I have a PHP class that stores database query results, but also contains a
I have a php class that generates a map image depending on my db
I have certain PHP class methods that access external variables. These variables are not
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have a php class, RegistrationController, <?php class RegistrationController extends ParentController { function __construct()
Let's say I have a PHP class called Color , it's constructor accepts various

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.