Help needed 3 tier-architecture

Reply

Join Date: Oct 2007
Posts: 78
Reputation: navi17 is an unknown quantity at this point 
Solved Threads: 5
navi17 navi17 is offline Offline
Junior Poster in Training

Help needed 3 tier-architecture

 
0
  #1
Jun 6th, 2009
Can someone present me an example which implements the three tier architecture in PHP. Let's say I have a class student with the fields ID and Name. I want to enter the student ID and Name through a web form and save it to the database. Could someone show me or provide me with a link to a PHP code example which shows the usage of the presentational, business and database layer in such a sitauation?

i dont want to use application frameworks.


thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: Baldy76 is an unknown quantity at this point 
Solved Threads: 3
Baldy76 Baldy76 is offline Offline
Newbie Poster

Re: Help needed 3 tier-architecture

 
0
  #2
Jun 6th, 2009
This is where I started and I would recommend it to anyone who asks, assuming that everyone else also learns best through trial and error.

http://www.google.com/#hl=en&q=php+%...fp=1mZ_-PL2Zjc
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: Help needed 3 tier-architecture

 
0
  #3
Jun 6th, 2009
Hi.

This is an example of a simple MVC application, just to give you an example of how it can be done.

I don't write out the base classes... no point really in this sample.
  1. <?php
  2. /**
  3.  * File: index.php
  4.  */
  5.  
  6. /* This assumes a file structure of:
  7.  * ----
  8.  * index.php
  9.  * MVC/
  10.  * Controllers/
  11.  * BaseController.php
  12.  * RandomController.php
  13.  * Models/
  14.  * BaseModel.php
  15.  * RandomModel.php
  16.  * Views/
  17.  * BaseView.php
  18.  * RandomModel.php
  19.  */
  20.  
  21. // Load the controller
  22. $ctrlDir = "MVC/Controllers/";
  23. ($page = @$_GET['page']) or $page = "";
  24. ($action = @$_GET['action']) or $action = "defaultAction";
  25.  
  26. $ctrlPath = $ctrlDir . ucfirst($page) ."Controller.php";
  27.  
  28. if(file_exists($ctrlPath)) {
  29. // Load and create a controller instance
  30. include($ctrlPath);
  31.  
  32. $ctrlName = ucfirst($page) . "Controller";
  33. $controller = new $ctrlName();
  34.  
  35. // Execute the selected action
  36. // Meaning, try to execute a method on the
  37. // controller that matches the $_GET['action']
  38. // value passed.
  39. $action = ucfirst($action);
  40. if(method_exists($controller, $action)) {
  41. $controller->$action();
  42. }
  43. else {
  44. // Execute the controllers default method
  45. $controller->DefaultAction();
  46. }
  47. }else {
  48. // You probably want to handle errors more elegantly ;)
  49. user_error("Error 404. The page you requested does not exist", E_USER_ERROR);
  50. }
  51. ?>
  1. <?php
  2. /**
  3.  * File: RandomController.php
  4.  */
  5.  
  6. /**
  7.  * A ranom controller that does random things.
  8.  * Extends the base controller, which should
  9.  * define anything that would be shared amongst
  10.  * all the Controllers.
  11.  *
  12.  * I am not going to define a base controller tho.
  13.  * I'll leave that up to you ;)
  14.  */
  15. class RandomController extends BaseController
  16. {
  17. protected $model;
  18. protected $view;
  19.  
  20. public function __construct() {
  21. // Normally these would be in a configuration
  22. // file somewhere as constants, but I'll just put
  23. // them here for this example.
  24. $modelDir = "MVC/Models/";
  25. $viewDir = "MVC/Views";
  26.  
  27. // Create the model
  28. $modelPath = $modelDir . "RandomModel.php";
  29. if(file_exists($modelPath)) {
  30. include($modelPath);
  31. $this->model = new RandomModel();
  32. }
  33. else {
  34. throw new Exception("Failed to load model");
  35. }
  36.  
  37. // Create the view
  38. $viewPath = $viewDir . "RandomView.php";
  39. if(file_exists($modelPath)) {
  40. include($modelPath);
  41. $this->model = new RandomModel();
  42. }
  43. else {
  44. throw new Exception("Failed to load model");
  45. }
  46. }
  47.  
  48. /**
  49.   * The default action.
  50.   * Used if the client doesn't specify a action,
  51.   * or of the client specifies a invalid action.
  52.   */
  53. public function DefaultAction() {
  54. // Get the name of the current user and show a message
  55. $username = $this->model->GetUsername();
  56. $this->_view->ShowDefaultMessage($username);
  57. }
  58.  
  59. /**
  60.   * A action.
  61.   * This one doesnt' really do much, but you get the point :P
  62.   */
  63. public function DoStuff() {
  64. // Get number of rows and show the message
  65. $dbNumRows = $this->model->GetTablebaseRowCount();
  66. $this->view->ShowDoStuffMessagE($dbNumRows);
  67. }
  68. }
  69. ?>
  1. <?php
  2. /**
  3.  * File: RandomView.php
  4.  */
  5.  
  6. /**
  7.  * A ranom view.
  8.  * Handles the presentation of the Random page.
  9.  */
  10. class RandomView extends BaseView
  11. {
  12. /**
  13.   * Shows a default welcome message.
  14.   * @param string $username The name to put into the welcome message.
  15.   */
  16. public function ShowDefaultMessage($username=null) {
  17. // Show a welcome message
  18. $username = ($username ? $username : "visitor");
  19. echo "<h1>Welcome to the default page, $username.</h1>";
  20.  
  21. // Show something else
  22. echo "Chose an action: <br />";
  23. echo " - <a href='?page=random&amp;action=doStuff>Do Stuff</a>";
  24. }
  25.  
  26. /**
  27.   * Display the output for the DoStuff action.
  28.   * @param <type> $rowCount
  29.   */
  30. public function ShowDoStuffMessage($rowCount=0) {
  31. echo "You have chosen to do stuff?!<br />";
  32. echo "Fine. I'll tell you how many rows we have in the database...<br />";
  33. echo " - Row count: {$rowCount}. <br />";
  34. echo "Happy now!";
  35. }
  36. }
  37. ?>
  1. <?php
  2. /**
  3.  * File: RandomModel.php
  4.  */
  5.  
  6. /**
  7.  * A random model.
  8.  * Handles all the database interaction for the Random page.
  9.  */
  10. class RandomModel extends BaseModel
  11. {
  12. protected $dbLink;
  13.  
  14. public function __construct() {
  15. // This would be done in the BaseView, but I'm just gona
  16. // do it here instead.
  17. $this->dbLink = new mysqli("localhost", "usr", "pwd", "db_name");
  18. if(mysqli_connect_errno()) {
  19. throw new Exception("Failed to establish database link: ". mysqli_connect_error());
  20. }
  21. }
  22.  
  23. /**
  24.   * Gets the name of the current user... not that there
  25.   * is anything written in here to keep track of users :P
  26.   *
  27.   * @return mixed The username on success, or FALSE on failiure.
  28.   */
  29. public function GetUsername() {
  30. // Do some query to get the users name.
  31. // I didn't add any sort of user tracking system, so this is just random :P
  32. $sql = "SELECT username FROM usrTable WHERE something='somethingElse'";
  33. $result = $this->dbLink->query($sql);
  34.  
  35. if($result || $result->num_rows > 0) {
  36. $row = $result->fetch_assoc();
  37. return $row['username'];
  38. }
  39. else {
  40. return false;
  41. }
  42. }
  43.  
  44. /**
  45.   * Returns the number of rows in the 'someTable' table.
  46.   * @return int
  47.   */
  48. public function GetTablebaseRowCount() {
  49. $sql = "SELECT COUNT(*) FROM someTable";
  50. $result = $this->dbLink->query($sql);
  51.  
  52. if($result) {
  53. return $result->num_rows;
  54. }
  55. else {
  56. return 0;
  57. }
  58. }
  59.  
  60. }
  61. ?>
Let me know if something is unclear or if I left something out.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Help needed 3 tier-architecture

 
0
  #4
Jun 6th, 2009
MVC is different from 3-tier from what I have read. MVC uses a triangle type layout where 3-tier is linear (I put this in his other thread, don't see why this one was created).
Last edited by kkeith29; Jun 6th, 2009 at 5:29 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: Help needed 3 tier-architecture

 
0
  #5
Jun 6th, 2009
Originally Posted by kkeith29 View Post
MVC is different from 3-tier from what I have read. MVC uses a triangle type layout where 3-tier is linear (I put this in his other thread, don't see why this one was created).
True, but notice how I've made the View unaware of the Model.
I've effectively mutated my MVC from the typical triangle into a more linear mode, making the controller responsible for controlling all interaction between the view and the model, rather than only being responsible for relaying changes in the view to the model.

It never made sense to me to have the view connect to the model anyways... makes it that much harder to control the flow of it all.

In any case, one could argue that MVC is a type of tier-3 architecture (that's how I see it, anyways).
The definitions for both are purposefully vague.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: Help needed 3 tier-architecture

 
0
  #6
Jun 6th, 2009
Actually, after digging a little deeper into the differences between n-tier architectures and MVC design patters, what I posted earlier is most likely mislabeled.

A tier-3 design pattern separates the buisness logic, the presentation layer, and the data layer and stacks them in a linear order, where each layer would only be able to communicate with the layers directly above or below it in the line.

This would put the buissnes logic in the middle, able to communicate with both the data layer and the presentation layer, but the latter two would only be able to communicate with the buisnes layer.

Which is pretty much exactly what the code I posted does.

A MVC pattern, on the other hand, defines three separate "modules" of sorts, each responsible for a specific part of the process, but each communicating with the others. (The communication triangle)
  • The Model; representing the application data. Receiving updates from the Controller, and providing data to the View.
  • The View; presents the application data to the "user". In the case of websites, the HTML output. It would receive data from the Model and present it to the "user". In the case of interactive UIs, the View would also pass on any user interaction to the Controller. (Button clicks and such)
  • The Controller; representing the buisness logic. Receives events from the View, interprets them and triggers updates in the Model, which are then reflected by the View.

So, according to my current interpritation of the two design patters, my MVC design example would in fact really be a tier-3 design patten.
(Which should not be confused with a tier-3 architectural pattern, which is apparently something completely different)
Last edited by Atli; Jun 6th, 2009 at 9:26 pm. Reason: Spelling
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 78
Reputation: navi17 is an unknown quantity at this point 
Solved Threads: 5
navi17 navi17 is offline Offline
Junior Poster in Training

Re: Help needed 3 tier-architecture

 
0
  #7
Jun 8th, 2009
thanks for the reply Atli.
actually i didnot understand your code at all.
it seems very complex to me.

can you provide me simple example?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 78
Reputation: navi17 is an unknown quantity at this point 
Solved Threads: 5
navi17 navi17 is offline Offline
Junior Poster in Training

Help needed 3 tier-architecture

 
0
  #8
Jun 8th, 2009
lets take a simple example:
classperson.php

  1. Class Person{
  2.  
  3. Protected $Name, $Age;
  4.  
  5. Public Function __Construct($Name, $Age){
  6.  
  7. $this->SetName($Name);
  8.  
  9. $this->SetAge($Age);
  10.  
  11. }
  12.  
  13. Public Function SetName($Name){
  14.  
  15. $this->Name = $Name;
  16.  
  17. }
  18.  
  19. Public Function SetAge($Age){
  20.  
  21. $this->Age = $Age;
  22.  
  23. }
  24.  
  25. Public Function SetName($Name){
  26.  
  27. Return $this->Name;
  28.  
  29. }
  30.  
  31. Public Function GetAge($Age){
  32.  
  33. Return $this->Age;
  34.  
  35. }
  36.  
  37. Public Function __toString(){
  38.  
  39. return <<<DETAILS
  40. Name: {$this->Name}
  41.  
  42. Age: {$this->Age}
  43. DETAILS;
  44. }
}

queryperson.php

  1. Public Function Save() {
  2. $Query=mysql_Query('insert into Person(name,age)
  3. values ($Name,$Age)';
  4. Return $query;
  5. }


personresult.php

  1. $Dave = new Person('Dave', 32);
  2. $Dave.Save()

above are three files..
am i right in above example?

if any changes required in the code you can do?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: Help needed 3 tier-architecture

 
0
  #9
Jun 8th, 2009
Why is your Save() method in a separate file?
If it is supposed to be a part of the Person class, it should be defined inside it.

Also, in PHP, you call class functions and variables using an arrow -> , not a dot.
Like:
  1. $person = new Person('John Doe', 15);
  2. $person->SetAge(35);
  3. $person->Save();

I'm getting the feeling that your trying to use C/C++ style OOP design.

You should go over the PHP5 OOP pages in the manual, to get familiar with how OOP works in PHP.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 78
Reputation: navi17 is an unknown quantity at this point 
Solved Threads: 5
navi17 navi17 is offline Offline
Junior Poster in Training

Re: Help needed 3 tier-architecture

 
0
  #10
Jun 8th, 2009
that was just a simple example. i want to show u.

ok..
i want to apply above example using 3 tier architecture.
pls modify above code with 3 tier implementation.
nd pls tell me about each layer?
Last edited by navi17; Jun 8th, 2009 at 8:18 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC