I am currently trying to rewrite a large amount of procedural code on my website into classes, so I can reuse them and to simplify some scripts that are becoming just incredibly huge. For most basic things, this isn't too difficult, like database wrappers, a login class, template classes, etc...
However now I'm trying to represent data sets in my database with classes. For example, let's say I have 3 tables in my database: "Sports", "Teams" & "Players". There can be many sports, each sport can have many teams and each team can have many players (Many "1 to many" relationships in my database). I wish to create a class for each of these, called "Sport", "Team" & "Player". The problem is a lot of my current programming on my website is using libraries of functions and queries which are combined with many joins. I started writing a few classes to represent these, however when I get to calling data from the database and planning how I will implement these classes in the long run, I just don't see a clear way to do it, and do it efficiently.
I stumbled upon this, which is about the same problem I'm having, but I didn't really get much out of reading the responses:
http://www.daniweb.com/forums/thread221776.htmlSo far I've been making my database calls occur when the object/class is loaded, but normally that entity/object ends up needing data from many other tables. I've also thought about Extending the classes or just making them aggregate classes (which store other classes in themselves in arrays), like the following example...
Note: this is straight from pg. 119 of the book "Object Oriented PHP: Concepts, Techniques and Code" by Peter Lavin.
<?php
class Player{
private $name;
private $position;
public function __construct($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
public function setPosition($position){
$this->position = $position;
}
}
class Team{
private $players = array();
private $name;
public function __construct($name){
$this->name = $name;
}
public function addPlayer(Player $p){
$this->players[] = $p;
}
public function getPlayers(){
return $this->players;
}
public function getName(){
return $this->name;
}
?>
- <?php
- class Player{
- private $name;
- private $position;
- public function __construct($name){
- $this->name = $name;
- }
- public function getName(){
- return $this->name;
- }
- public function setPosition($position){
- $this->position = $position;
- }
- }
-
- class Team{
- private $players = array();
- private $name;
- public function __construct($name){
- $this->name = $name;
- }
- public function addPlayer(Player $p){
- $this->players[] = $p;
- }
- public function getPlayers(){
- return $this->players;
- }
- public function getName(){
- return $this->name;
- }
- ?>
I've also thought about just somehow gathering my data outside of the class and then putting it in, which makes no sense what-so-ever for what I'm doing. When I google anything to do with OOP/data/MySQL optimization, the results are flooded with database wrapper examples instead of what I need to know. Does anybody know a book where I could learn how to better go about doing this or have a way they like to do it?
There's no place like 127.0.0.1, badass part is now it's ::1