A bit confused about OOP and PHP
I have programmed with procedural style a lot before and in these few months I have decided to go OOP.
Im a bit confused about OOP and PHP. Lets say I have Categories table and Pages table in my database. I would like to do a page-class and a category-class.
Lets say I would have a page that showed information about the category and there would be a list of pages that belong to the category. How would I implement that in OOP way?
I have thought that the category class would have a property (array) that would contain all the pages that it has. Well then comes the part that I`m confused about. Should I have a method that would get all the pages with correct categoryID from the database and should I instantiate all the rows as an Page-object?
Or should I have a setPages-method in the Category-class and getAllPages-method in the Pages-class and with t开发者_如何学JAVAhat method I would set all the pages to the Category-class.
Huh Im lost. :D
Seems like you are mixing databases and OOP. I would suggest that you first model your classes in an object oriented way and then worry about the database. To answer your question, you should definitely have a unique object for every Page and every Category.
For the database part, I would recommend you use the Data Mapper pattern (http://martinfowler.com/eaaCatalog/dataMapper.html) or the ORM (http://en.wikipedia.org/wiki/Object-relational_mapping) pattern.
You do not have to load all pages and data from database. The common solution is to use so called Active records. One object per one database row. For example:
class Page
{
var $id;
var $title;
var $txt;
function __construct($id) {...} // load from database
function save() {...} // save to database edited data
}
You can create class Record, for example:
class Record
{
var $tablename; // table name
var $row; // associative array - represents a row from a database
function __construct($tablename, $id)
// standard use cases with database
function save(); // will save a row to the database table
function load(); // will retrieve a row from the database table
function delete();
}
Usage:
$page = new Record('page', 10);
echo $page['title'];
This is very simple solution only for example. See CodeIgniter Active Records for better example.
Create a Page class.
Create a Category class. One of its attributes should be an array of Page objects named $pages (or similar).
Create a method in the Category class called getPages()
which will return the $pages array. If you like, this can be a "lazy-loading" method that gets the pages from the DB when it is called and caches them in the $pages attribute. Or you can load all the Page objects when the Category object is created. Your call.
Create a method in the Category class called setPages($newArray)
if you need or want to. This method should be used to overwrite the $pages array attribute with the value of the $newArray parameter.
setX() methods are (traditionally) used to set the values of attributes, not to kick off DB lookups or other data generation schemes. They'll look like this most of the time:
public function setPages($newArray)
{
// data checking here to make sure $newArray is valid
$this->pages = $newArray;
}
Object oriented programming is large topic which is not as language specific as you would think.
You have an excellent tutorial for OOP in PHP and I'd suggest to you to go here and read it: OOP Part 1 and OOP Part 2.
For PHP development I would strongly suggest to you the switch to an OOP style of development:
- You will write less code.
- It will be easier to maintain.
- It will be easier to extend.
So would it be correct if I would do it like this:
For example:
$category = new Category();
$category->setPages(); // This would get all the pages (with the right categoryID) from the database and istantiate every row as an object to an array in the Category-class.
Then i could do like:
foreach($category->getPages() as $page) {
echo $page->getName()."<br>";
}
精彩评论