International Developer Logo Last Updated 25.07.08 at 11.48
On Sale
This months front cover, click to see the table of contents.
Subscribe
 
TUTORIALS

Developing Three-Tier Web Applications with PHP


Daniel Winter   01.10.05

As you can see, the application object is identical to the database object. So, instead of assigning each property individually as is done above, the following can be used instead.

   function select($theid){
      $db = getDbConn();
      $sql = "select id,subject,teacherid from
subjects where id = ?";
      $data = array($theid);
      $res =& $db->query($sql, $data);
      if (DB::isError($res)) {
          die ($res->getMessage());
      }
      if($res->fetchInto($row, DB_FETCHMODE_OBJECT)){
   //must return row object as $this cannot be set here
        return $row;
      }      
   }

Because you cannot use $this = $row from within a class method, the object is simply returned to the calling object as shownhere.

$mySubject = new trainingsubject;
$mySubject = $mySubject->select(1);


For more complex structures that aren't mirrored exactly by database tables, your application objects will require some customisation, so you won't always be able to reuse the database objects. For instance, our application will require a student class that incorporates all fields from the student's table as well as those from the address table. Should the application grow to require multiple addresses per student record, the student object can be modified to include an array of address objects as one of its properties. Similarly, a student will also need an array of subject objects assigned to a class property. This way, the application will be able to create a student object and then iterate through the subjects property to manipulate the enrolments for that student.


Step 5 - Presentation Layer Templating with Smarty
With the bulk of the application wrapped-up in classes and functions, the only thing left to do is provide a user interface! Historically, web applications have used embedded HTML code in dynamic script pages - whether the scripting language is ASP, PHP, Perl, ColdFusion or whatever. By separating the HTML code into templates, however, it is possible to develop the application within a simple wireframe HTML template and then pass the templates on to the designers and front end developers at a later date. This not only means that the PHP code is protected from the front end developers, it also means you don't need to be involved whenever the client decides to change elements of the graphic design.




   Previous Page  1 2 3 4 5 6 7 8 9 10 ... Next Page   

HAVE YOUR SAY
This article is rated  Rate this article