The system utilizes a single User class and most of the method utilize PDO to exchange the data with the database.
class User {
private $db_server;
private $db_login;
private $db_pass;
private $db;
public function method_1(){
$statement = new PDO("mysql:host=".$this->db_server.";dbname=".$this->db, $this->db_login, $this->db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
//doing something with PDO
}
// ...
public function method_n(){
$statement = new PDO("mysql:host=".$this->db_server.";dbname=".$this->db, $this->db_login, $this->db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
//doing something with PDO
}
}
The question is am I utilizing PDO effectively? Should I instantiate a PDO object as one of the class properties and pass it to every method that needs it, instead of instantiating a new PDO object in the method itself?
you should take a look at Dependency Injection.
The main idea is, that you inject the a composed and complete PDO-Object (the dependency) whilst creating User. Try to configure PDO only once in your application and get it through singleton-pattern. This way you have only this one instance.
E.g.
$pdo = new PDO($connString);
$user = new User($pdo);
Be sure to add an object property to User, so you can access $this->pdo.
This is only one approach how you could solve your problem, but it is not the most efficient way.
If you are going to use the principle of the best practice way, you will not use a PDO-Object in User at all. Instead you would build an Object-Relational-Mapper (ORM). In this approach you would have a central Entity-Manager which holds the Database Adapter, in your case pdo. This entity-Manager would take User and can apply database operations on it, such as delete, create, update. You would define a general mapping, so that object properties equals table columns.
Feel free to ask if you need more information :)