4
\$\begingroup\$

I am learning PDO. I have a class - User...

class User {
    protected static $table_name="users";
    public $id;
    public $first_name;
    public $last_name;

    public function pdo_create() {
        global $handler;
        $sql = " INSERT INTO users (first_name, last_name)"; 
        $sql .= " VALUES (?, ?)";
        $query = $handler->prepare($sql);;
        $query->execute(array($this->first_name ,$this->last_name));
    }
}

When I run this...

$user= new User();
$user->first_name = "test";
$user->last_name = "test";
$user->pdo_create();

It works fine. I know it's a bit odd posting when things are working, but I have suspicions that this is not the best approach. I am still learning PDO and about the convert my MySQLi site over to PDO. I want to make sure the PDO veterans think this is a reasonable approach.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! Working code is exactly what we're about! \$\endgroup\$ Apr 24, 2014 at 12:23
  • \$\begingroup\$ @SimonAndréForsberg basically critic his code because he is new to PDO. Read my rebuttal for him \$\endgroup\$
    – azngunit81
    Apr 24, 2014 at 12:30

1 Answer 1

5
\$\begingroup\$

Your design is flawed in the following way:

DO NOT USE GLOBAL.

It is not OO programming and you shouldn't be calling it if you are using PDO - pass your PDO through your function or as part of your user constructors. (see SOLID design patterns)

class User 
{
     //private attributes
     private $_db;
     public function __construct(PDO $pdo)
     {
         $this->_db = $pdo;
     }
}

your function pdo_create is too couple with pdo - if you decide to switch off from pdo - you will need to re-write that function call to reflect what you change to - use convention a bit more general: creatingUser(PDO $pdo) for example or even better creatingUser(DBInterface $db) (as part of SOLID

Now as for the function itself:

+1 for using prepare statement however ? is more used for mysqli - it better to use place handler instead. such as VALUES(:first_name, :last_name). When you pass the array you can then use the place handler to effectively assign to which variable its value. It is best for readability later.

Finally: you defeat the purpose of the class if your attributes are PUBLIC. Ideally functions receives parameters by the function itself OR by the constructor of the class which initializes the attributes of the class.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ azngunit81 - forgive my ingnorance, but could you give me an example using this case of "pass your PDO through your function or as part of your user constructors". I'd be really grateful.. \$\endgroup\$
    – GhostRider
    Apr 24, 2014 at 12:36
  • \$\begingroup\$ @GhostRider I added a bit of code - but fill out the rest on your own. \$\endgroup\$
    – azngunit81
    Apr 24, 2014 at 12:42
  • \$\begingroup\$ many thanks I can see what way to go now... \$\endgroup\$
    – GhostRider
    Apr 24, 2014 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.