PHP Oriented Web Application

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

Each page I create creates a new layout.

When a user logs in, a new user is created.

How do I get an instance of a layout class to find out about an instantiated user? I could also save the entire user instance in a session variable. I guess this is a bad idea. What are the best methods for doing 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 say that in login.php someone is logged in:

 include "layout.php" include "user.php" if(userAuthSuccess) { $currentUser = new User($_POST['username']); } 

What is the best way to access $ currentUser and its member variables like $ currentUser-> email, etc. from all php files from here if the user has not logged out?

+4
source share
5 answers

Since there will be only one User for each request and, therefore, for each launch of your program, this will be the case when you make the "User" class Singleton, as described here:

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

This will provide one way for other classes to refer to the current user without the ability to access the wrong instance, since there is only one.

DISCLAIMER: Yes, I know that Singeltons are often used in the wrong places for the wrong purpose, and some people tend to blame this problem for the pattern, and not for people who misused it with some smelly code. This, however, is quite suitable for the Singelton template.

0
source

I think the best solution for the solution outlined above is for a concept called Dependency Injection, in which you write an additional class that will introduce a dependency (an object in this case) to the requesting class. Most modern developers will adhere to this technology for injecting dependencies into their applications, as this will allow:

Freely related programs. Since the dependency is introduced by the third class, there is no need to hard code the dependency in the program logic.

Supported Code This is the feature of the OOP paradigm that will charm the most. This is especially true if you turn to large-scale programs.

Memory management. As a developer, you can manage the memory according to your specification requirements.

+1
source

β€œGlobalizing” something by putting it in a session variable or cookie for the sole purpose of globalization is a very bad habit to enter, and this leads to tightly linked libraries that rely on an arbitrary variable set outside the class. Session variables are generally good to stay away from other reasons.

The best way to get a variable in any class is to pass it as an argument. Do you have a method in your Layout class that displays (displays) it? You can add the $ data argument to this method, which takes an associative data array that can be used in the layout.

0
source

I would personally use the registry class (Singleton) and register the user to access the layout. So you need to pass the registry instance to the layout. The User class is not an integral part of the Layout construct, since it should only be associated with the layout, so I would not pass this in the constructor.

Another method would be to use a controller to organize these interactions between views and models. Whenever a new controller is created, buffer its output. Then, during rendering, buffer the content and assign it to the properties (or array of properties) of the view, which can then display them. You probably don't need the actual Model class, which will be passed to the View / Layout - it only outputs.

0
source

Use a registry template. No need to make it a single, everybody drops that word.

 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 that you insert into it) through the data array.

0
source

Source: https://habr.com/ru/post/1310926/


All Articles