patternphpMinor
User and Database classes in PHP
Viewed 0 times
userphpdatabaseclassesand
Problem
I am learning some OOP on PHP and I have developed some sample code to practise.
Ignoring the functionality, I just want to learn the "best" or "better" way to write OOP codes in PHP.
The following is my code and I hope anyone who has been doing PHP can help to review or comment for any improvements or weaknesses on my code.
I want to develop my style of code in which copy-and paste is minimum and functionality expandability is rich. For example, the
How can I optimized my code to made it better for future functionalities such as other select, insert, update and delete while maintaining readability and still within OOP standards?
```
conn = new PDO ("mysql:host=".DB_LOCATION.";dbname=".DB_NAME,DB_USE
Ignoring the functionality, I just want to learn the "best" or "better" way to write OOP codes in PHP.
The following is my code and I hope anyone who has been doing PHP can help to review or comment for any improvements or weaknesses on my code.
I want to develop my style of code in which copy-and paste is minimum and functionality expandability is rich. For example, the
execute() function in the UserDB class is basically the same function and can be used widespread in many diff db class. Do I need to copy and paste the code again or should I just moved this piece of code to abstract class? How can I optimized my code to made it better for future functionalities such as other select, insert, update and delete while maintaining readability and still within OOP standards?
UserFactory classexecute();
if(!empty($objs))
foreach( $objs as $obj)
{
$users[] = new user($obj);
}
return $users;
}
}
?>UserDB classconn = parent::getConn();
$this->SQL = $SQL;
$this->para = $para;
}
function execute()
{
$query = $this->conn->prepare($this->SQL);
if(!empty($this->para))
foreach($this->para as $key=>$val)
{
$query->bindParam($key,$val);
}
$results = $query->execute();
$dataset = null;
if($results)
{
while($row = $query->fetch(PDO::FETCH_OBJ))
{
$dataset[]=$row;
}
}
unset($this->conn);
parent::__destruct();
return $dataset;
}
}
?>Database abstract class```
conn = new PDO ("mysql:host=".DB_LOCATION.";dbname=".DB_NAME,DB_USE
Solution
Just a few comments on the code as it stands so far:
I would say that on the whole you're on basically the right track though. It's certainly better than my own early efforts with OOP. Don't worry, thinking in an "objecty" way is something that comes with practice and experience.
- You're using pretty old-school file dependency management (which isn't the same as dependency management) by explicitly including things. I'd strongly suggest you look into the SPL autoload system, write an autoloader that can find where your classes are located in the filesystem and rely on the autoloader to handle the file dependency management instead. It means that only code that's actually going to be used will be included and you won't get into a situation where you have loads of included classes but only a few get used, or where you're not sure what's been included.
- You're using static methods. Generally, statics are frowned upon because they're not considered to be good OO practice. Sometimes they're necessary, but as a rule you should avoid them. Any other class that makes use of the static call becomes tightly coupled to the class that implements it, reducing re-usability and testability (it becomes very difficult to test the dependant modules in isolation because you can't easily replace the static with a mock implementation). I'd advise refactoring your factory to work in a non-static way.
- Your Database class seems like it might not be necessary. It's merely a wrapper around PDO, and actually hides most of the PDO class functionality. I'd suggest you might be better off using PDO directly, as it's got a well-documented interface and you'll have full access to all the PDO functionality in the consuming classes instead of the small subset your Database class provides.
I would say that on the whole you're on basically the right track though. It's certainly better than my own early efforts with OOP. Don't worry, thinking in an "objecty" way is something that comes with practice and experience.
Context
StackExchange Code Review Q#8823, answer score: 2
Revisions (0)
No revisions yet.