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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:35:33+00:00 2026-05-27T04:35:33+00:00

It seems quite simple but I’m not getting it done for quite a while

  • 0

It seems quite simple but I’m not getting it done for quite a while now…

class Main
{
    public $sub1;
    public $sub2;

    public function __construct()
    {
        $this->sub1 = new Sub1();
        $this->sub2 = new Sub2();
    }
}

class Sub1 extends Main
{
    public function __construct() {}

    public function helloWorld()
    {
        echo "hello world";
    }
}

class Sub2 extends Main
{
    public function __construct()
    {
        $this->sub1->helloWorld();
    }
}

new Main();

The result of the code is Fatal error: Call to a member function helloWorld(). But why? I don’t understand why the sub1 property of the Main class is NULL when I try to access it from sub2. Could anyone please explain it to me? Am I doing something basically wrong in terms of OOP programming? Am I hurting any concepts? Are there any better solutions?

Best regards!

  • 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-27T04:35:34+00:00Added an answer on May 27, 2026 at 4:35 am

    You are overriding Main’s __construct() in both Sub1 and Sub2, meaning Main’s construct is never called. Even if you were to call parent::__construct() in Sub1 and Sub2’s construct, you would get an infinite loop because Main’s construct is initializing both subs.

    Edit: Since you want to share objects, here are a couple SO questions that can help:
    Sharing objects between PHP classes
    How to create an object in a parent class shared by its children classes?

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

Sidebar

Related Questions

My problem seems to be quite simple, but it's not working the intuitive way.
This seems quite simple, but I can't figure it out. In PHP, I could
My problem seems to be quite simple, but I've been stuck on it for
What I'm trying to do is quite simple but as a beginner I'm getting
My question seems quite simple but became tedious for me. In my project, i
What I'm trying to accomplish seems quite simple however, I'm not great at jQuery.
This seems like it should be quite simple, but for some reason I can't
This one seems to be quite ridiculous but how can I open a VS
A question that seems to have quite a few options for Python, but none
I want to use JOGL (for Clojure, not Java). There seems to be quite

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.