PHP and PDO class question

后端 未结 3 1153
粉色の甜心
粉色の甜心 2020-12-18 06:53

I am very new to OOP styled PHP, and I am trying to implement PDO as well. I found this nice little class online which handles the database connection, however I have no ide

3条回答
  •  生来不讨喜
    2020-12-18 07:18

    If you really must do database work from inside your Person object (separation of concerns says that this should be taken care of elsewhere), you can either pass it as a constructor argument (based on your usage, it looks like that may be the way you want to go), or as a setter injection. For the former:

    class Person
    {
        function __construct($db_handle)
        {
            // ... your existing code
    

    Then you instantiate your person object like so:

    $person = new Person($db_handle);
    

    That's really the only way you can avoid needing to use $this->db_handler without copying the variable into local scope.

提交回复
热议问题