Answer the question
In order to leave comments, you need to log in
PHP OOP singleton, how to use static methods correctly?
Help me to understand. I am making a User class. How to properly organize the User class and then use it? I feel like I'm doing something wrong.
namespace model;
use \core\DB;
class User
{
protected static $_instance;
private $user_data;
public function __construct($user_id)
{
DB::$sql_query = "SELECT * FROM `table_users` where `id_usr` = '".$user_id."' ";
DB::sql_execute();
$this->user_data = DB::fetch_assoc();
}
public function getUser() {
return $this->user_data['name'];
}
core\DB::getInstance();
function __construct() // конструктор в модели
{
$this->model = new \model\model_place();
$this->view = new \view\view_index();
$this->user = new User('1234');
}
function action_index()
{
$data = $this->model->get_data();
echo $this->user->getUser(); // для примера
$this->view->generate('view_index.php', 'html_page.htm', $data);
}
Answer the question
In order to leave comments, you need to log in
1) It is better to use global services through Dependency Injection, otherwise there will be problems with writing tests.
2) User, like other entities of the subject area related to the database, is best done through DataMapper or ActiveRecord templates
3) You are unlikely to have a User when executing the code, you don’t need a singleton at all.
Why organize the User class through a singleton, what are your goals?
When I came across a ready-made ORM, I realized that I didn’t understand something, not in terms of how to use them, but how they are arranged. No wonder, because it was Doctrine2 coupled with Symfony.
After that, I decided to try to write something similar from scratch myself, as a result, a whole framework came out. I have a simple object mapper with repositories in the Core module - I built the entire framework on Dependency Injection.
Now I decided to sort through what I wrote, to cover it with tests - I started a blog to describe the whole process.
Try some sensible framework first. There you will learn in practice what and where to use and how it works.
You probably don't really need a singleton.
For robots, you can simply create a User class and static methods in it (for example getUser()) - User::getUser()
You probably wanted to say that the class is good, but a little refactoring will improve it. You are right - there is still work to be done to complete this class. I think that the study of the subject area can become the direction of refactoring. For example, User contains first name, last name, address, etc. I think that such fields should be in the class
class User
{
public $name;
public $family;
public $address;
}
class User
{
public $name;
public $family;
public $address;
private $user_data;
private static $_instance;
}
class User
{
public $name;
public $family;
public $address;
private $user_data;
private static $_instance;
public function Load(){}
public function Save(){}
}
class User
{
public $name;
public $family;
public $address;
private $user_data;
private static $_instance;
public function Load(){}
public function Save(){}
public function LoadData(){}
public function SaveData(){}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question