Object-Orientation transcends PHP and AJAX. To write an OO solution, you should first understand and describe the objects in the problem domain, along with their responsibilities.
A booking website, for example, might have classes such as:
- Appointment
- Calendar
- Client
- Vendor
Describe the responsibilities of these classes. For example, client objects might have the ability to schedule an Appointment with a particular Vendor. This might look like:
client.schedule( appointment, vendor )
The responsibilities and collaborators that you describe should be applicable to any OO language. Once you have defined the basic classes (user stories can help here), start thinking about how to move the data from the user interface into the domain classes, and vice-versa.
One idea, that skips the MVC controller, is to give the responsibility of reading and writing data to the class that holds the data. Consider:
class Client {
function toJSON() {
// Create a JSON string.
}
function fromJSON( json ) {
// Set the local variables here.
}
}
Note that how the data is transformed into XHTML remains independent of the model at this point. That is, toJSON()
could just as easily be toXML()
or toString()
.
I would then make session-based authenticated services for queries. For example, to retrieve a list of cities, make a file called city.php
:
<?php
include_once( 'db.php' );
try {
$link = db_connect();
$name = strtolower( db_safe( $_GET[ 'q' ] ) );
$name = empty( $name ) ? '' : $name;
db_json( "SELECT c.name WHERE c.lowercase_name LIKE '%$name%' LIMIT 10" );
db_close( $link );
}
catch( Exception $ex ) {
db_close( $link );
}
?>
Here, the database functions (such as db_connect(...)
) could be encapsulated in a class, then that class is used to query the database. Notice that using a class like this would help decouple the code from a particular database.
These services would allow you to query various pieces of information. The user interface, being XHTML and JavaScript, could then use jQuery or other such framework to handle client-side events. For example, to present a Facebook-like tokenized input field UI for selecting a city:
$('#city').tokenInput( HOST + 'city.php', {
hintText: "Type a city name.",
tokenLimit: 1,
classes: {
tokenList: "token-input-list-facebook",
token: "token-input-token-facebook",
tokenDelete: "token-input-delete-token-facebook",
selectedToken: "token-input-selected-token-facebook",
highlightedToken: "token-input-highlighted-token-facebook",
dropdown: "token-input-dropdown-facebook",
dropdownItem: "token-input-dropdown-item-facebook",
dropdownItem2: "token-input-dropdown-item2-facebook",
selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
inputToken: "token-input-input-token-facebook"
}
});
Lastly, implement handlers for the various inputs. These handlers would be responsible for parsing the form data (from HTTP request parameters to the format expected by the class instances [i.e., JSON]), instantiating the requisite objects, and then asking the objects to perform the appropriate task.