-1

I'm working on a PHP MVC but struggling to handle certain aspects of the design, making the User information accessible easily to everything is really puzzling me.

I did have User as an abstract class so you could just use User::getUserId() but I'm not sure that was the right way to do it?

I've now changed it to the following but it's not working.

The Problem

Say I have a basic controller where I want to instantiate the User class every time it's run:

Controller

class controller{

    public $user;

    function __construct(){

        $this->user = new User();
        $user->sessionRefresh();
        $user->getUserId();

    }
}

and then I have a User model class that was instantiated:

User Model Class

class User{

    var $userId;

    function sessionRefresh(){

        if (isset($_SESSION['userId']) && $_SESSION['created'] + 15 * 60 < time())                  
            session_regenerate_id();
    }

    function getUserId(){

        $this->userId = $_SESSION['userId'];
    }
}

and then I have more controllers that extend the default controller:

Forum Controller - Very basic just to demo what I'm trying

class forumcontroller extends controller{

    function __construct(){

        require 'templates/forum.php';
    }

}

Forum Template - Very basic example

<html>
<title>Forum</title>
<body>
    <div>Welcome User: <?=$user->userId;?></div>
</body>
</html>

Is what I'm doing completely wrong?

I've only recently tried to take the leap from procedural to mvc/oop in PHP and clearly it's confusing me still.

Dan
  • 11,914
  • 14
  • 49
  • 112

2 Answers2

2
  1. Use $this->user->userId; instead of $user->userId;

  2. You have to echo .... <? echo $user->userId; ?>

  3. Stop editing your code while someone is helping

  4. Thx for Downvote

Dan
  • 11,914
  • 14
  • 49
  • 112
Dukeatcoding
  • 1,363
  • 2
  • 20
  • 34
  • @Dukeatcoding: The editor here at SO is a bit buggy. You have to enter things in a special way to get the result you want. You cannot start with a numbered list, for example. – JvdBerg Oct 30 '12 at 16:51
  • What's the difference between `$this->user->userId;` and `$this->$user->userId;`? – Dan Oct 30 '12 at 16:53
  • @JvdBerg thx for mentioning it, when I post a lot of code it is always not really easy to get it a little bit formated and i don't see a sense in formating to much at all for this little things – Dukeatcoding Oct 30 '12 at 16:53
  • @Silver89 maybe you are right here $this->$user->userid; might be right (http://php.net/manual/en/language.oop5.php) – Dukeatcoding Oct 30 '12 at 16:57
0

Yes, what you are doing is completely wrong, when it comes to MVC design pattern. Partially it is related to the fact, that MVC is an advanced pattern. You should not even be trying to use it until you have solid understanding of OOP.

So .. the problems with your code.

  • When you use include or require, the file which you "embedded" is ins the scope in which it was used. Read about variable scope and template

  • Model is not a class or an object. It is a layer, which contains multiple structures of different types of structures. This should cover the short version.

  • Interaction with User instance would be part of domain business logic, which is responsibility of model layer. None of this should be in the presentation layer (which is mostly composed of views, controllers and templates).

  • View is not a template. In MVC design pattern views are structures which are responsible for most of user interface logic. The decide what sort of response should be returned (wither a HTML composed from multiple templates, some XML or JSON file, maybe just a HTTP header).

  • The var statement was used in PHP4 to define class variables. In PHP5 we have public, private and protected.

  • In general, you should not have public variables in class. The break the encapsulation.

  • Class constructors should not perform computation. It makes the code hard to test.

Update

The bottom line is this: **stay away from MVC* for at least 3 more month. You need to learn a lot more about OOP before you can fully utilize and understand MVC design pattern.

I would recommend the list of materials at the bottom of this post. Start with the first two books, then watch the lectures and then the PoEAA (last one in the list) book.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150