A4ec51727310397c9e592dd84ae74dc2

Hi guys,

I'm trying write PHP code that is easier to use.

I have seen a number of approaches, including traditional MVC, but I came up with this one approach where I construct object that deliver FORMATTED HTML to the page that calls it. So basically in this approach, the class User is ALL things: model, view and controller.

Normally people don't do it this way... or at least, I haven't seen examples of it. Normally people will use the class to retrieve and store data, but NOT to display it. In this version, the class does everything from retrieve the data to deliver the formatted html.

comments?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
# file:  User_class.php
class User extends DatabaseConnectable
{
  private $userId;
  private $userName;
  private $fullName;
  private $imgAvatarURL;

  function __construct( $id )
  {
    # retrieve all info about user 
    # from db and save into member vars $userName
    # etc.
  }

  function display()
  {
    echo( '<table>' );
    echo( '<tr><td>' );
    echo( "user:  $userName" );
    echo( '</td><td>');
    echo( "<img src=\"$imgAvatarURL\" />" );
    echo( '</td></tr>' );
    echo( '</table>' );
  }
}
?>


# Then, in the rendering page...

1
2
3
4
5
6
<?php

$user = new User( $_SESSION['userId'] );
$user->display();

?>

Refactorings

No refactoring yet !

1e6b9d50678a0cc32d98aa1dd1ec3846

TiTi

August 1, 2008, August 01, 2008 14:39, permalink

No rating. Login to rate!

Everything is not in the class user : your controller (what you call "the rendering page") is outside !

It's a bad idea to do that way, for instance :
-How could you change the rendering of a user and another differently ?
-How could you display an array of users ?
-The designer need to know your classes to modify the display

I recommend you a template system like TinyButStrong for example : http://www.tinybutstrong.com/
I'm actually writing a tutorial (aldready 6 parts written) to create a structured website in PHP 5 Object Oriented + TinyButStrong.... Stay tuned it will be published in some days/weeks on the TinyButStrong website :)

To give you a simple example i joined the first demo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php 

class student 
{ 
    public $IdStudent; 
    public $NameStudent; 
    public $AgeStudent; 
} 

$guy = new student(); 
$guy->IdStudent = 5; 
$guy->NameStudent = 'WOOD'; 
$guy->AgeStudent = 22; 

include_once('includes/tbs_class_php5.php'); 
$TBS = new clsTinyButStrong; 
$TBS->LoadTemplate('demo1.html'); 
$TBS->Show(); 

?>
1e6b9d50678a0cc32d98aa1dd1ec3846

TiTi

August 1, 2008, August 01, 2008 14:40, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Demo 1</title>
</head>
<body>

<b>ID :</b> [var.guy.IdStudent]<br />
<b>NAME :</b> [var.guy.NameStudent]<br />
<b>AGE :</b> [var.guy.AgeStudent]

</body>
</html>
E7b27313f932222f22beaa3686746bd3

Piccolo Principe

August 5, 2008, August 05, 2008 11:23, permalink

No rating. Login to rate!

Your class infringes the Single Responsibility Principle: one class should do one thing, and do it well. That promotes reuse of code: you can reuse a model class for the user to generate an rss or xml file, and reuse views of a user to view other data like a list of them. And the class is difficult to test because it doesn't produce a data structure but a ugly html...

Your refactoring





Format Copy from initial code

or Cancel