Im working with php for about 4 years now, but i still havent really used an OOP approach on any of my own scripts. I would really like to use it though, because for waht i heard and what i understand it makes developing much easier. For example the following code, it is a simple mysql-select, getting categories and from the results i am building a form-element, a dropdown-menu, there are a few steps involved like fe sorting stuff with natcasesort() too. Atm i would redo the same procedure for countries, and i would write the same code again, only that categories would be replaced with country, as you can see:
$result1 = mysql_query("SELECT * from categories WHERE hidden = 0");
while ($row = mysql_fetch_array($result1)) {
$cat_names = explode(",",$row['title_mulilingual']);
$categories[$row['uid']] = $cat_names[$lang_id];
}
natcasesort($categories);
foreach ($categories as $key => $value) {
if ($key == $active_key) $selected = ' selected="selected"';
else $item_selected = '';
$select_fields['cat_id'] .= '<option'.$selected.' value="'.$key.'">'.$value.'</option>';
}
$cat_select = '<select name="category" >'.$select_fields['cat_id'].'</select>';
// the following part is the same as the first one, it only handles countries instead of categories
$result2 = mysql_query("SELECT * from countries WHERE hidden = 0");
while ($row = mysql_fetch_array($result2)) {
$country_names = explode(",",$row['title_mulilingual']);
$countries[$row['uid']] = $country_names[$lang_id];
}
natcasesort($countries);
foreach ($countries as $key => $value) {
if ($key == $active_key) $item_selected = ' selected="selected"';
else $selected = '';
$select_fields['country_id'] .= '<option'.$selected.' value="'.$key.'">'.$value.'</option>';
}
$country_select = '<select name="country" >'.$select_fields['country_id'].'</select>';
So in the code it obviously is not OOP. But im guessing, that i could do this much easier by using OOP, right? Could someone help me to understand OOP better, by making this example an OOP-example? Thanks in advance, Jayden
Could someone help me to understand OOP better, by making this example an OOP-example?
Hey you could easily port this into OOP with a framework like MVC.
(source: php-html.net)
"Model–View–Controller (MVC) is a design pattern for computer user interfaces that divides an application into three areas of responsibility:
- the Model: the domain objects or data structures that represent the application's state.
- The View, which observes the state and generates output to the users.
- The Controller, which translates user input into operations on the model." (Wikipedia, 2012)
Some really good OOP frameworks already available.
A good tutorial on how to create a Model View Control can be found here.
Controller
This retrieves the data and then sends it to the model to be processed.
include_once("model/Model.php");
class Controller {
public $model;
public function __construct()
{
$this->model = new Model();
}
public function invoke()
{
if (!isset($_GET['book']))
{
// no special book is requested, we'll show a list of all available books
$books = $this->model->getBookList();
include 'view/booklist.php';
}
else
{
// show the requested book
$book = $this->model->getBook($_GET['book']);
include 'view/viewbook.php';
}
}
}
Model
It processes the data to be presented
include_once("model/Book.php");
class Model {
public function getBookList()
{
// here goes some hardcoded values to simulate the database
return array(
"Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),
"Moonwalker" => new Book("Moonwalker", "J. Walker", ""),
"PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")
);
}
public function getBook($title)
{
// we use the previous function to get all the books and then we return the requested one.
// in a real life scenario this will be done through a db select command
$allBooks = $this->getBookList();
return $allBooks[$title];
}
}
class Book {
public $title;
public $author;
public $description;
public function __construct($title, $author, $description)
{
$this->title = $title;
$this->author = $author;
$this->description = $description;
}
}
View
Presents the data.
<?php
echo 'Title:' . $book->title . '<br/>';
echo 'Author:' . $book->author . '<br/>';
echo 'Description:' . $book->description . '<br/>';
?>
from my experience I find its easier to do OOP using a framework than learning it from scratch in web applications.
Theres also a good tutorial on creating your own framework at nettuts that can be found here, If you don't understand the above information this tutorial isn't to overwhelming.