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();
}
}
?>
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..