2

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.

4
  • I think you're confused about what $this means. Commented Oct 21, 2012 at 1:35
  • didn't know what to put there. I'm trying to pass the object through the class and access it somehow Commented Oct 21, 2012 at 1:36
  • 1
    I asked much the same question a long time ago: stackoverflow.com/questions/2862399/… Commented Oct 21, 2012 at 1:37
  • :O I read that question before I posted this one. I wasn't sure if it were the same thing, I will look into it again Commented Oct 21, 2012 at 1:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

oh wow after testing the instance there is a lot of power in this... you can call functions and set variables through the instance. Pretty incredible. Thank you Waleed this is exactly what I needed.
@Adam184 This pattern is called dependency injection, which is a pretty useful technique to know.
1

The class One does not know about the $User variable and therefore can not access it.

You could either pass the variable to the class in the constructor or set it after creation of an object of the class.

class One extends Base {
    protected $user;
    public function setUser(User $user) {
        $this->user = $user;
    }
    public function One(User $user) {
        $this->setUser($user);
    }
    public function test() {
        print_r($this->user->variable);
    }
}

Or (but please don't, it is bad practice) set the $User variable to global.

global $User

2 Comments

Thanks, that's why I titled the question "passing the object User through the One class".
As alternative to the 'global' option you can set $variable static. The you'd call print_r(User::$variable);. But although not so bad as 'global', still not a good practice. Use the inject option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.