Dan
Dan

Reputation: 12096

Do I have the wrong idea about PHP inheritance?

I'm trying to create a simple example php script, there is a user class and then two controllers.

Imagine it as an MVC with no view.

At the moment I cannot get getUserId() to work within showUserId() in usercontroller, what am I doing wrong?

PHP

<?
class user{

    function getUserId(){

        return 1; //Simple Example
    }
}

class controller{

    function __construct(){

        $user = new User();
    }
}

class usercontroller extends controller{

    function __construct(){

        $user = new User();
    }

    function showUserId() {

        echo $user->getUserId();
    }
}
?>

Upvotes: 0

Views: 73

Answers (4)

Ja͢ck
Ja͢ck

Reputation: 173522

I would take out the $user variable from controller, because I don't feel it really belongs there:

class controller
{
}

Then, inside usercontroller I would pass a user object as a dependency:

class usercontroller extends controller
{
    private $user;

    function __construct(User $user)
    {
        $this->user = $user;
    }

    public function showUserId() 
    {
        echo $this->user->getUserId();
    }
}

$c = new usercontroller(new User());
echo $c->showUserId();

Upvotes: 1

Rajat Singhal
Rajat Singhal

Reputation: 11254

class usercontroller extends controller{

    private $user;

    function __construct(){

        $this->user = new User();
    }

    function showUserId() {

        echo $this->user->getUserId();
    }
}

You need to define the $user as class attribute of the controller class, define it in constructor, then you can use it in all functions..

Edit;

Yes you can define in oarent and call parent's constructor in child constructor, and use it..

<?
class user{   
    function getUserId(){    
        return 1; //Simple Example
    }
}

class controller{
    protected $user;
    function __construct(){    
        $this->user = new User();
    }
}

class usercontroller extends controller{

    function __construct(){
        parent::__construct();
        //$user = new User();
    }

    function showUserId() {    
        echo $this->user->getUserId();
    }
}

$controller_instance = new usercontroller();
$controller_instance->showUserId();    
?>

Upvotes: 1

David Hamp
David Hamp

Reputation: 151

In your code example, $user exists as a local variable in the __construct() function.

If you want it to be a part of the class first define a class property with that name, then you can reference it by using the $this-> syntax.

class usercontroller extends controller {
    public $user;

    function __construct(){
        $this->user = new User();
    }

    function showUserId() {
        echo $this->user->getUserId();
    }
}

As a nit picky side note, your 'User' class is defined as 'user' in your code example. I'm sure you intended those to match as you weren't asking about parse errors with your script.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

$user is only available in the __construct scope. To be available in the object scope it has to be bound to the object as a member:

$this->user = new User;

Then, you can access $this->user.

It is also recommended to include this member as part of the class definition not only for clarity but also to set the correct access modifier.

Upvotes: 1

Related Questions