G
G
grabbee2014-06-02 15:53:06
Design patterns
grabbee, 2014-06-02 15:53:06

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']; 
    }

The DB class is made in the style of a singleton; it is initialized once.
core\DB::getInstance();
Here's where I start to get confused. How to properly organize the User class and then use it? Now in the model I do this, but it seems to me that this is somehow not right
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);  
    }

UPD: using a singleton in User is an error, corrected.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexander Kubintsev, 2014-06-02
@grabbee

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.

P
Pavel Solovyov, 2014-06-02
@pavel_salauyou

Why organize the User class through a singleton, what are your goals?

Y
Yakov Akulov, 2014-06-02
@jakulov

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.

K
Konstantin Andreevich, 2014-06-02
@reffy

Try some sensible framework first. There you will learn in practice what and where to use and how it works.

K
kambur, 2014-06-03
@kambur

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()

M
Maxim Gavrilov, 2015-02-26
@thestump

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;
}

User does something and operates with data, so it must have a data field and there can be only one in the User system:
class User 
{        
    public $name;
    public $family;
    public $address;

    private $user_data;     
    
    private static $_instance; 
}

There may be more fields, but for now let's stop. Think what the class can do!? User can be loaded and can be saved
class User 
{        
    public $name;
    public $family;
    public $address;

    private $user_data;     
    
    private static $_instance; 

    public function Load(){}
    public function Save(){}
}

Can load data from the database and upload data to the database:
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(){}
}

Etc. Then it is recommended to write a unit test for one of the methods and, using testing and refactoring, write the code for this method, then the next one, and so on. until the end of the class. Move the work to the ORM area, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question