php - OOP eficiency when using a class in another class -


afternoon, have class called db (class.pdo.php) handling on mysql queries using pdo , class called user use manage login system.

my question relates having instantiate $db in every public function of users can use db. efficient? shouldn't instantiating db inside __construct() of users?

this code

 require_once("../../class.pdo.php");  class user {  private $db = null;  public function __construct(){     /* empty? */ }  public function find_by_email($email){     $db = new db();     $db->query('select * users email = :email limit 1');     $db->bind(':email',$email);     $result = $db->single();     return $result; }  public function create($email,$password,$first_name,$last_name){      $db = new db();     $db->query("insert users(email,password,first_name,last_name,created_at) values (:email,:password,:first_name,:last_name,now())");     $db->bind(':email',$email);     $db->bind(':password',$password);     $db->bind(':first_name',$first_name);     $db->bind(':last_name',$last_name);     $result = $db->execute();     return $db->lastinsertid(); }   [more similar functions ommited] 

what using singleton pattern create 1 object connection , use everytime need it, instead of creating new objects time?


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -