I am finally making the move to object oriented PHP. This has been something I have been quite reluctant to do, but I now feel that it could benefit me, and bring me up-to-date with current practices.
The main problem I have is how to start, and how to do things right. Now, I understand that with programming, there are many ways to do things, but I like to do things the correct, and efficient way.
The project
One of the reasons I wish to make the move to OOP is that myself and my colleagues at work wish to try coding this way to update an existing, and antiquated system that manages employee details. We have said that if we don't start using OOP, we never will!
It will eventually become a nice MVC and AJAX rich web-based app for our intranet (The MVC is a whole different story for another post!).
The code
The following code is my first attempt at writing a class to retrieve employee data from a MySQL database using the PDO class. I have tried to adhere to PSR-2 coding style where possible, but if you see anything out of place, then please say.
Employee.php
<?php
class Employee
{
private $db;
private $employee;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function fetchBy($value, $type = 'username')
{
$stmt = null;
switch ($type) {
case 'username':
$stmt = $this->db->prepare("SELECT * FROM `employees` WHERE `Username` = :value");
$stmt->bindValue(':username', $value, PDO::PARAM_STR);
break;
case 'id':
$stmt = $this->db->prepare("SELECT * FROM `employees` WHERE `ID` = :id");
$stmt->bindValue(':id', $value, PDO::PARAM_INT);
break;
case 'department':
$stmt = $this->db->prepare("SELECT * FROM `employees` WHERE `Department` = :department");
$stmt->bindValue(':department', $value, PDO::PARAM_INT);
}
$stmt->execute();
if ($stmt->rowCount() > 1) {
$this->employee = $stmt->fetchAll(PDO::FETCH_OBJ);
} else {
$this->employee = $stmt->fetch(PDO::FETCH_OBJ);
}
}
public function fetchEmployee()
{
return $this->employee;
}
}
index.php
<?php
spl_autoload_register(function ($class) {
include 'class/'.$class.'.php';
});
try {
$db = new PDO('mysql:host=localhost;dbname=employees','user','password');
} catch (PDOException $e) {
// For now, print the message during dev, but later, log this!
print $e->getMessage();
die();
}
$employee = new Employee($db);
$employee->fetchBy("1", "department");
$fetchEmployee = $employee->fetchEmployee();
if (is_array($fetchEmployee)) {
foreach ($fetchEmployee as $feItem) {
echo $feItem->FirstName." ".$feItem->Surname."<br />";
}
} else {
echo $fetchEmployee->FirstName." ".$fetchEmployee->Surname;
}
$db = null;
Questions
- What do you think of my code, honestly? Does it look like I am on the right tracks?
- In regards to the PDO class, I understand that with it being a class itself. Should I just try and use it as-is without trying to wrap it, or should I actually wrap it? Would that make it easier to perform some tasks?
- As you will see, I am using a
SELECT * FROM
for querying the table. The idea behind this is to load up all the fields into an object that I can pick data out to display. Usually I prefer specifying each column, but is there any point to that? An alternative was to specify a LOT of getter methods for each field. Would that be a good idea, or overkill? - Is the
fetchBy
method a good idea or, am I on the path to ending up with a query builder? Would that be a bad thing? I've read about dependency injection, so I am keeping the database object separate and loading this into the Employee object. Is the way I am doing this using the __construct method the right way or should I do this with another method like this?
Employee.php
public function setDb(PDO $db) { $this->db = $db; }
index.php
$employee = new Employee; $employee->setDB($db);
Feel free to pick my code apart and be constructive with your criticism. I can take it. I would rather pick up good habits now, than go full steam ahead doing things badly, and end up having to fix them later!