开发者

PHP Object Oriented Web Application

I have a class called "Layout" for the layout of the page, another class called "User" for the user.

Every page I create, I instantiate a new Layout.

When a user logs in, there is a new User instantiated.

How do I get an instance of the layout class to know about the instantiated user? I could also save the entire instance of the User in a session variable. I assume that's a bad idea though. What are the best practices for this?

class User
{
  var $userid;
  var $email;
  var $username;

  function User($user)
  {
     $this->userid = $this->getUid($user);
     $this->email = $this->getEmail($user);
     $this->username = $user;
  }
  function getUid($u)
  {
     ...
  }

  function getEmail($u)
  {
     ...
  }
}

class Layout
{

    var $var1;
    var $var2;
    var $var3;

    function Layout()
    {
        //defaults...
    } 


    function function1()
    {
        echo "something";
    }


   function function2()
   {
        echo "some other stuff";
   }

   function function3()
   {
      echo "something else";
   }

}

so in index.php, for example, i would do the following:

include "user.php"
include "layout.php"

$homelayout = new Layout();
$homelayout->function1();
$homelayout->function2();
$homelayout->function3();

now let's say that in login.ph开发者_开发百科p someone logged in:

include "layout.php"
include "user.php"

if(userAuthSuccess)
{
    $currentUser = new User($_POST['username']);
}

what is the best way to gain access to $currentUser and it's member variables such as $currentUser->email, etc. from all php files from here on out, as long as the user hasn't logged out?


I think the best remedy to the solution stated above is going for a concept called Dependency Injection, whereby you write an extra class, that will inject the dependency (An Object in this case) to the requesting class. Most of the modern developers will adhere to using this technique of injecting dependencies into their applications as this will enable:

Loosely coupled programs - As the dependency is injected by a third class, there is no need to hard code the dependency in the logic of the program.

Maintainable code - This is that feature of the OOP paradigm that allures the most. This is especially true when referring to large scale programs.

Memory Management - As a developer, you are free to manage the memory to your specification requirements.


Since there will be only one User for every request and thus for every run of your program, this would be a case to make "User" a Singleton class as described here:

http://php.net/manual/en/language.oop5.patterns.php

That would provide the one way for other classes to refer to the current user without the chance of accessing the wrong instance since there is only one.

DISCLAIMER: Yes, I know that Singeltons are often used at the wrong places for the wrong purpose and some people tend to blame this problem on the pattern instead of the people who misused it for some smelly code. This however is a perfectly good use case for the Singelton pattern.


"Globalizing" something by putting it in a session variable or cookie for the sole purpose of globalizing it is a very bad habit to get into, and it leads to tightly coupled libraries that rely on an arbitrary variable being set outside the class. Session variables in general are good to stay away from for other reasons, too.

The best way to get a variable into any class is to pass it as an argument. Do you have a method in your Layout class that renders (outputs) it? You may want to add a $data argument to that method that takes an associative array of data usable in the layout.


I'd personally use a registry class (Singleton) and register the user there for the Layout to access. That way, you only need to pass an instance of the registry to the Layout. The User class is not integral to the Layout's construction - since it should only be concerned with the Layout, so I wouldn't pass that in the constructor.

Another method would be to use a Controller to orchestrate these interactions between Views and Models. Whenever a new Controller is created, buffer it's output. Then, at render time, unbuffer the contents and assign them to properties (or a property array) of the view, which can then render them. You probably don't need an actual Model class to be passed to the View/Layout - just it's output.


Use a registry pattern. No need to make it a singleton, everyone is throwing that word around.

include "layout.php"
include "user.php"

if(userAuthSuccess)
{
    $data['currentUser'] = new User($_POST['username']);
}

$data['nav'] = new nav('home'); 

$homelayout = new Layout( $data );

Now $homelayout can access $data (which contains all the variables you put into it) via the data array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜