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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:51:06+00:00 2026-06-13T02:51:06+00:00

After restructuring my entire class layout, I’m still having trouble using multiple class instances.

  • 0

After restructuring my entire class layout, I’m still having trouble using multiple class instances.

class User {
    public $variable;
    public function getUser() {
        $this->variable = 'It works!';
        return 'bob';
    }
}

class Base {}
class One extends Base {
    public function test() {
        print_r($User->variable); // Want it to print 'It works!'
    }
}

$User = new User();
$username = $User->getUser();
if($username === 'bob') {
    $One = new One();
    $One->test(); // prints "Notice: Undefined property: One::$variable
}

What the above code does: The class User gets the username. If the username is bob, it will create an object one and try to print the variable from class User. In my real code, User is extremely intricate and passing a bunch of things with __construct just isn’t what I’m looking for.

I think the solution (which is why I titled this question as so) would be to create a new class User within class Base but I just can’t wrap my head around having to create a whole new object and re-initiate everything just to get the username.

  • 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-13T02:51:06+00:00Added an answer on June 13, 2026 at 2:51 am

    Inject your User instance:

    class One {
        private $user;
        public function __construct(User $user) {
            $this->user = $user;
        }
        public function test() {
            print_r($this->user->variable);
        }
    }
    
    $User = new User();
    $One = new One($user);
    $One->test();
    

    This doesn’t have any undue effects with regards to efficiency, as the instance is passed by reference as opposed to copied.

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

Sidebar

Related Questions

After a particular query using PHP, I have a $result variable with only 1
After some time researching and trying different things I still cannot get my @ExceptionHandler
After a long search I'm still confused about it although I found some related
After autheticating my user, I want to put a reference in the session to
After a user logs in to an app I am developing, based on their
After following: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview I can sign up a user via Facebook. But I'm struggling
After using Instruments, I have found the a spot in my code that is
After updating from ruby-1.8.7-p352 to ruby-1.8.7-p358 via rvm , gems are still installed to
After constructing the bean, I want to retrieve data from the database, using the
I am using Entity Framework and recently came to realize the benefits of having

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.