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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:53+00:00 2026-06-02T03:38:53+00:00

I know this may seem like a strange question, but say I have two

  • 0

I know this may seem like a strange question, but say I have two classes;

class Class1 {
    private $foo;
    function addBar(Class2 &$bar)
    {
        $this->foo = $bar;
    }
}


class Class2 {
    private $foo
}

$a = new Class1;
$b = new Class2;
$a->addBar($b);

Is there any way Class2 can read information from Class1? Even though they’re in the same code block here, they are in seperate files in my script. The reason I need this is because I have Class2 linked by reference, but Class2 isn’t allowed to be run until Class1 has been, and I know I could pass another variable to reference Class2 to Class1 (eg. $b->addFoo($a)), but I would rather avoid that if possible.

I hope this can be done!
Thanks in advance

EDIT

Thanks to everyone who helped, I have decided to go another way around it. Thankyou again.

  • 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-02T03:38:55+00:00Added an answer on June 2, 2026 at 3:38 am

    Class2 can call methods on Class1 objects, though it can’t access private data. But it must have a reference to a Class1 instance in order to call any methods on it.

    Your example doesn’t show Class2 actually containing any code, and it sounds like you’re trying to avoid calling methods on $b. That doesn’t make sense. Class2 can’t “do” anything if you never call any methods on it.

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

Sidebar

Related Questions

I know this may seem like a math question but i just saw this
Now this may seem like a silly question, but I need to know how
This may seem like a stupid question, but I need to know where the
This may seem like a dumb question, but still I don't know the answer.
This may seem like a very simple question, but I have been struggling with
This may seem like a silly question, but I'd like to know the best
I know this may seem like a common question on SO but I could
This may seem like an odd question, but does anyone know the origin of
This may seem like a dumb question but I don't know how to do
I know the title may seem strange but this is what I want to

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.