<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users707friends</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/707/friends" rel="self"/>
  <title>Ishkur friends</title>
  <updated>Wed Aug 06 16:57:00 +0000 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14569</id>
    <published>2008-08-06T16:57:00+00:00</published>
    <title>[PHP] On MySQL original PHP 5 wrapper class</title>
    <content type="html">&lt;p&gt;Woa! Very interesting way to do select queries with the where clause. And still the advantage of my orm : getWhere work for every single class your implementing...
&lt;br /&gt;Thanks for paying attention to my code and refactor it!
&lt;br /&gt;I don't have time to look for orm systems like coughphp or phpdoctrine because i'm not working on a php project at the moment and i'm very busy, but i think they are more optimized...
&lt;br /&gt;But as I said, I think it might be easier in the future to use my class with php6 (after some lifting...)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/416-mysql-original-php-5-wrapper-class/refactors/14569" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14387</id>
    <published>2008-08-02T19:38:44+00:00</published>
    <title>[PHP] On MySQL original PHP 5 wrapper class</title>
    <content type="html">&lt;p&gt;Well basically I don't use the @ operator because it suppresses errors. To answer your question, I'm handling errors with my own exception class. .... hum ok I got it, you add the @ here to respond to my original question : &amp;quot;stupid to launch an exception here ?&amp;quot; ... hum no, i got it NOW ! without the @ operator, the warning/fatal error is not suppressed... Damn I didn't think of that this way. I never used @ in order to the see errors but you're right : here i'm using exceptions but i'm completely missing thing kind of extraneous errors. Thanks for telling me that!
&lt;br /&gt;Ok i google it and this article explains how to deal with those extraneous errors : &lt;a href="http://thesmithfam.org/blog/2006/05/07/php-the-operator/" target="_blank"&gt;http://thesmithfam.org/blog/2006/05/07/php-the-operator/&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/416-mysql-original-php-5-wrapper-class/refactors/14387" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14366</id>
    <published>2008-08-02T10:41:05+00:00</published>
    <title>[PHP] On MySQL original PHP 5 wrapper class</title>
    <content type="html">&lt;p&gt;I don't think it's a good idea to use the '@' operator, see : &lt;a href="http://michelf.com/weblog/2005/bad-uses-of-the-at-operator/" target="_blank"&gt;http://michelf.com/weblog/2005/bad-uses-of-the-at-operator/&lt;/a&gt;
&lt;br /&gt;Maybe it doesn't slow down the execution with the mysql_close command, but as a rule of thumb, i don't use @.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/416-mysql-original-php-5-wrapper-class/refactors/14366" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code416</id>
    <published>2008-08-01T20:44:15+00:00</published>
    <updated>2008-08-14T07:20:25+00:00</updated>
    <title>[PHP] MySQL original PHP 5 wrapper class</title>
    <content type="html">&lt;p&gt;Typical MySQL wrapper written in PHP use functions such as : Query($sql), FetchRow(), ...
&lt;br /&gt;My approach is different.
&lt;br /&gt;The idea behind my mySQL class is that you just have to create your own classes and call protected methods to do the mapping with the database, without a single line of SQL in your classes.
&lt;br /&gt;For instance, with my mySQL wrapper, you just have to code classes like the student class.
&lt;br /&gt;If you look carefully, there is really nothing to code : mostly only verifications in the setter... sweet!&lt;/p&gt;

&lt;p&gt;My vision was to add a single static property to your own class : the table name the class refers to.
&lt;br /&gt;Unfortunaltely, I was not able to do that with PHP 5,  you have to add others variables in the class.
&lt;br /&gt;In fact my wrapper requires that you :
&lt;br /&gt;-subclass your own classes with my mysql abstract class (i know it's wired)
&lt;br /&gt;-define the following members in your class :
&lt;br /&gt;	$_table
&lt;br /&gt;	$_fields
&lt;br /&gt;	$_primaryKey
&lt;br /&gt;	$_loadFields
&lt;br /&gt;-call getFields(); and getPrimaryKey(); in the constructor (see my student class example to get an idea)&lt;/p&gt;

&lt;p&gt;Then you just have to call the protected functions... the student class is showing what you can do.&lt;/p&gt;

&lt;p&gt;//-------------------------&lt;/p&gt;

&lt;p&gt;Please note that:
&lt;br /&gt;-i'm using utf8 in the connecting method
&lt;br /&gt;-i'm saying &amp;quot;A primary key is never NULL&amp;quot; ; i know this is not true, but its crappy if you have null primary key, really.
&lt;br /&gt;-every mysql call is done for a precise connection link (self::$db_link) =&amp;gt; could enhance the code to use severals databases
&lt;br /&gt;-interesting cheat using eval used all over the class : eval('return '.get_class($this).'::$_primaryKey;');&lt;/p&gt;

&lt;p&gt;Recommendations:
&lt;br /&gt;-use your own exception class
&lt;br /&gt;-have a look at &lt;a href="http://www.phpdoctrine.org/" target="_blank"&gt;http://www.phpdoctrine.org/&lt;/a&gt; and &lt;a href="http://coughphp.com/" target="_blank"&gt;http://coughphp.com/&lt;/a&gt; (i think those are way better than my crappy code, i've JUST discover the ORM concept ^^)&lt;/p&gt;

&lt;p&gt;//-------------------------&lt;/p&gt;

&lt;p&gt;Where I need help:&lt;/p&gt;

&lt;p&gt;-Get rid of the statics properties and functions in the student class
&lt;br /&gt;I think that's really hard to do so with PHP 5, maybe PHP 6 will help us doing such kind of stuff (&amp;quot;static::&amp;quot;)&lt;/p&gt;

&lt;p&gt;-Considering charge issues : is it better to use a function like `getFields()`, or to put fields manually in the class `$_fields = array('f1','f2',...);` ?
&lt;br /&gt;I think it's better manually because you don't ALTER a table very oftenly...
&lt;br /&gt;but for a large / dynamic system ... i don't know, what do you think ?&lt;/p&gt;

&lt;p&gt;-look at disconnect() in the mySQL class ; what do you think of the comment (the exception if mysql_close returns an error) ?&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

abstract class mySQL // abstract =&amp;gt; cannot be instanciated
{
	const db_host = 'localhost';		// host
	const db_base = 'MYBASE';		// database  
	const db_user = 'MYUSER';		// user
	const db_pass = 'THEPASSWORD';		// password
	private static $db_link = false;	// link
	private static $db_connected = false;	// connection established?
	
	//------------------------------
	// Constructor + Destructor
	
	public function __construct()
	{
		self::connect();
	}
	
	public function __destruct()
	{
		self::disconnect();
	}
	
	//------------------------------
	// Connect + Disconnect
	
	public static function connect()
	{
		if(!self::$db_connected)
		{
			self::$db_link = mysql_connect(self::db_host, self::db_user, self::db_pass);
			if(!self::$db_link)
				throw new Exception('Database connection error :-(');
			
			if(!mysql_select_db(self::db_base, self::$db_link))
				throw new Exception('Database selection error :-(');
			
			if(!mysql_query(&amp;quot;SET NAMES 'utf8'&amp;quot;, self::$db_link))
				throw new Exception('Impossible to use utf8 to communicate with the database :-(');
			
			self::$db_connected = true; // We're good !
		}
	}
	
	public static function disconnect()
	{
		if($db_connected)
			mysql_close(self::$db_link); // whatever the return value... (stupid to launch an exception here ?)
	}
	
	//------------------------------
	// Insert + Update + Delete
	
	protected function insert_bdd()
	{
		if(!$db_connected) // useless here because of child classes constructor (getFields &amp;amp; getPrimaryKey -&amp;gt; connect)
			self::connect();
		
		$table = eval('return '.get_class($this).'::$_table;');
		if(!$table)
			throw new Exception('Don\'t know what table to use for '.get_class($this).' :-(');
		
		$fields = eval('return '.get_class($this).'::$_fields;');
		if(!$fields)
			throw new Exception('Don\'t know what fields describe '.get_class($this).' :-(');
		$tb_champs = array_fill_keys($fields, 1);
		
		$txt_fields = '';
		$txt_values = '';
		$i = 0;
		
		foreach($this as $key =&amp;gt; $value)
		{
			if(isset($tb_champs[$key]))
			{
				if($i &amp;gt;0)
				{
					$txt_fields .= ',';
					$txt_values .= ',';
				}
				$txt_fields .= $key;
				
				$c = self::quote_smart($value);				
				if(is_numeric($value))
					$txt_values .= $c;
				elseif(is_null($value))
					$txt_values .= 'NULL';
				else
					$txt_values .= &amp;quot;'$c'&amp;quot;;
				
				$i++;
			}
		}
		
		$sql = 'INSERT INTO '.$table.'('.$txt_fields.') VALUES ('.$txt_values.');'; 
		if(!mysql_query($sql, self::$db_link))
			throw new Exception('Insertion error:&amp;lt;br /&amp;gt;'.$sql.'&amp;lt;br /&amp;gt;'.mysql_error(self::$db_link));
		return mysql_insert_id(); // don't forget that to update your object
	}
	
	protected function update_bdd()
	{
		if(!$db_connected) // useless here because of child classes constructor (getFields &amp;amp; getPrimaryKey -&amp;gt; connect)
			self::connect();
		
		$table = eval('return '.get_class($this).'::$_table;');
		if(!$table)
			throw new Exception('Don\'t know what table to use for '.get_class($this).' :-(');
		
		$fields = eval('return '.get_class($this).'::$_fields;');
		if(!$fields)
			throw new Exception('Don\'t know what fields describe '.get_class($this).' :-(');
		$tb_champs = array_fill_keys($fields, 1);
		
		$txt_requete = 'UPDATE '.$table;
		$i = 0;
		
		foreach($this as $key =&amp;gt; $valeur)
		{
			if(isset($tb_champs[$key])) // Si le champ existe
			{
				if($i == 0)
					$txt_requete .= ' SET ';
				else
					$txt_requete .= ',';
				
				$txt_valeur = self::quote_smart($valeur);
				if(is_null($valeur))
					$txt_requete .= &amp;quot;$key=NULL&amp;quot;;
				else
					$txt_requete .= &amp;quot;$key='$txt_valeur'&amp;quot;;
				$i++;
			}
		}
		
		$i = 0;
		$primaryKey = eval('return '.get_class($this).'::$_primaryKey;');
		
		foreach($primaryKey as $key)
		{
			if($i == 0)
				$txt_requete .= ' WHERE ';
			else
				$txt_requete .= ' AND ';
			
			$c = self::quote_smart($this-&amp;gt;$key); // __get() could throw an exception if this field doesn't exist
			$txt_requete .= &amp;quot;$key='$c'&amp;quot;; // A primary key is never NULL
		}
		
		$txt_requete .= ';';
		if(!mysql_query($txt_requete, self::$db_link))
			throw new Exception('Update error:&amp;lt;br /&amp;gt;'.$txt_requete.'&amp;lt;br /&amp;gt;'.mysql_error(self::$db_link));
		if(mysql_affected_rows(self::$db_link) == 0) // Primary Key does not match any record =&amp;gt; exception
			throw new Exception('Update error:&amp;lt;br /&amp;gt;'.$txt_requete.'&amp;lt;br /&amp;gt;0 records affected');
	}
	
	protected function delete_bdd()
	{
		if(!$db_connected) // useless here because of child classes constructor (getFields &amp;amp; getPrimaryKey -&amp;gt; connect)
			self::connect();
		
		$table = eval('return '.get_class($this).'::$_table;');
		if(!$table)
			throw new Exception('Don\'t know what table to use for '.get_class($this).' :-(');
		
		$primaryKey = eval('return '.get_class($this).'::$_primaryKey;');
		if(!$primaryKey)
			throw new Exception('Don\'t know what are primary key fields for '.get_class($this).' :-(');
		
		$t = array();		
		foreach($primaryKey as $key =&amp;gt; $value)
			$t[$value] = $this-&amp;gt;$value;
		
		self::deleteDirectly($table, $primaryKey, $t);
	}
	
	//------------------------------	
	// Init an object from its primary key
	
	protected function init_by_primaryKey($Pk)
	{
		if(!$db_connected) // useless here because of child classes constructor (getFields &amp;amp; getPrimaryKey -&amp;gt; connect)
			self::connect();
		
		$table = eval('return '.get_class($this).'::$_table;');
		if(!$table)
			throw new Exception('Don\'t know what table to use for '.get_class($this).' :-(');
		
		$Pkfields = eval('return '.get_class($this).'::$_primaryKey;');
		if(!$Pkfields)
			throw new Exception('Don\'t know what are primary key fields for '.get_class($this).' :-(');
		
		// To be sure $Pk is filled with enough keys to describe a primary key, we have to verify
		$Pkfields = array_flip($Pkfields);
		if(count(array_intersect_key($Pk, $Pkfields)) != count($Pkfields))
			throw new Exception('Primary key fields does not match those of table '.$table);
		
		$req = 'SELECT * FROM '.$table;
		
		$i = 0;
		foreach($Pk as $key =&amp;gt; $value)
		{
			if($i == 0)
				$req .= ' WHERE ';
			else
				$req .= ' AND ';
			
			$c = self::quote_smart($value);
			$req .= &amp;quot;$key='$c'&amp;quot;; // A primary key is never NULL
		}
		
		$res = mysql_query($req);
		if(!$res)
			throw new Exception('Invalid request to init by primary key');
		if($d = mysql_fetch_object($res))
		{
			foreach(get_object_vars($d) as $var =&amp;gt; $value)
				$this-&amp;gt;$var = $value; // call __set
		}
		else
			throw new Exception('No record for this primary key :-(');
	}
	
	//------------------------------
	// Get the primary key
	
	protected function getPrimaryKey()
	{
		if(!$db_connected)
			self::connect();
		
		$table = eval('return '.get_class($this).'::$_table;');
		$keys = array();
		
		$result = mysql_query('SHOW KEYS FROM '.$table, self::$db_link);
		if(!$result)
			throw new Exception('Impossible to get primary key(s) of table '.$table);		
		while($row = mysql_fetch_assoc($result))
		{
			if ($row['Key_name'] == 'PRIMARY')
				$keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
		}
		
		return $keys;
	}
	
	//------------------------------	
	// Get the fields
	
	protected function getFields()
	{
		if(!$db_connected)
			self::connect();
		
		// Can't use self::$_table ; so here is a nice cheat :
		$table = eval('return '.get_class($this).'::$_table;');
		$tb = array();
		
		$result = mysql_query('SHOW COLUMNS FROM '.$table, self::$db_link);
		if(!$result)
			throw new Exception('Impossible to get information about table '.$table);		
		while($row = mysql_fetch_assoc($result))
			$tb[] = $row['Field'];
		
		return $tb;
	}
	
	//------------------------------
	// Static function to remove a record from database
	
	protected static function deleteDirectly($table, $Pkfields, $Pk)
	{
		if(!$db_connected)
			self::connect();
		
		// Cannot use get_class($this) to get the table because we're in a static function, so i'm using a parameter... same thing with $Pkfields
		
		// To be sure $Pk is filled with enough keys to describe a primary key, we have to verify
		$Pkfields = array_flip($Pkfields);
		if(count(array_intersect_key($Pk, $Pkfields)) != count($Pkfields))
			throw new Exception('Primary key fields does not match those of table '.$table);
		
		$txt_requete = 'DELETE FROM '.$table;
		$i = 0;
		
		foreach($Pk as $key =&amp;gt; $value)
		{
			if($i == 0)
				$txt_requete .= ' WHERE ';
			else
				$txt_requete .= ' AND ';
			
			$c = self::quote_smart($value);
			$txt_requete .= &amp;quot;$key='$c'&amp;quot;; // A primary key is never NULL
		}
		
		$txt_requete .= ';';
		if(!mysql_query($txt_requete, self::$db_link))
			throw new Exception('Delete error:&amp;lt;br /&amp;gt;'.$txt_requete.'&amp;lt;br /&amp;gt;'.mysql_error(self::$db_link));
		if(mysql_affected_rows(self::$db_link) == 0) // Primary Key does not match any record =&amp;gt; exception
			throw new Exception('Delete error:&amp;lt;br /&amp;gt;'.$txt_requete.'&amp;lt;br /&amp;gt;0 records affected');
	}
	
	//------------------------------
	// getAll + getCount
	
	protected static function getAll($class, $table)
	{
		if(!$db_connected)
			self::connect();
		
		$etu = array();
		
		$res = mysql_query('SELECT * FROM '.$table);
		if(!$res)
			throw new Exception('Impossible to retrieve all items of '.$table);
		while($d = mysql_fetch_object($res))
		{
			$e = new $class;
			foreach(get_object_vars($d) as $var =&amp;gt; $value)
				$e-&amp;gt;$var = $value; // call __set
			$etu[] = $e;
		}
		
		return $etu;
	}
	
	protected static function getCount($table)
	{
		if(!$db_connected)
			self::connect();
		
		$result = mysql_query('SELECT COUNT(*) AS nb FROM '.$table);
		if(!$result)
			throw new Exception('Impossible to count items of '.$table);
		$row = mysql_fetch_assoc($result);
		return $row['nb'];
	}
	
	//------------------------------
	// SQL protection
	
	private static function quote_smart($value)
	{
		if(get_magic_quotes_gpc())
			$value = stripslashes($value);
		
		if(!is_numeric($value))
			$value = mysql_real_escape_string($value);
		
		return $value;
	}
}

?&amp;gt;

&amp;lt;?php

/*
CREATE TABLE `phepsyl`.`Students` (
`IdStudent` INT NOT NULL AUTO_INCREMENT ,
`NameStudent` VARCHAR( 50 ) NOT NULL ,
`AgeStudent` INT NOT NULL ,
`PictureStudent` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `IdStudent` ) 
) ENGINE = InnoDB
*/

class student extends mySQL // student is a child of mySQL
{
	// Must-have members of the class
	public static $_table = 'Students';		// table name
	public static $_fields = array();		// fields of the table
	public static $_primaryKey = array();	// the primary key
	private static $_loadFields = false;	// are fields of this table aldready loaded?
	
	//------------------------------
	// Properties of the class
	
	protected $IdStudent;
	protected $NameStudent;
	protected $AgeStudent;
	protected $PictureStudent;
	
	//------------------------------
	// Constructor + Destructor
	
	public function __construct()
	{
		parent::__construct(); // call dady
		
		if(self::$_loadFields == false)
		{
			// Only once during execution : we get the table fields and the primary key of the concern table (here=Students)
			
			self::$_fields = self::getFields();
			self::$_primaryKey = self::getPrimaryKey();
			self::$_loadFields = true; // done
		}
		
		foreach($this as $key =&amp;gt; $value)
			$this-&amp;gt;$key = NULL; // set every member to NULL
	}
	
	public function __destruct()
	{	
		foreach($this as $key =&amp;gt; $value)
			unset($this-&amp;gt;$key);
	}
	
	//------------------------------
	// Getter + Setter
	
	public function __get($attribute)
	{
		if(!property_exists(get_class($this), $attribute))
			throw new Exception('Trying to get an invalid student member');
		
		return $this-&amp;gt;$attribute;
	}
	
	public function __set($attribute, $value)
	{
		if(!property_exists(get_class($this), $attribute))
			throw new Exception('Trying to set an invalid student member');
		
		// Verifications here...
		if($attribute == 'AgeStudent' &amp;amp;&amp;amp; (!is_numeric($value) || $value &amp;lt; 0))
			throw new Exception('Invalid student age: '.$value);
		
		$this-&amp;gt;$attribute = $value;
	}
	
	//------------------------------
	
	public function init_student($IdS, $Name, $Age, $Picture) // Copy constructor
	{
		$this-&amp;gt;IdStudent = $IdS;
		$this-&amp;gt;NameStudent = $Name;
		$this-&amp;gt;AgeStudent = $Age;
		$this-&amp;gt;PictureStudent = $Picture;
	}
	
	public function insert()
	{
		$this-&amp;gt;NameStudent = strtoupper($this-&amp;gt;NameStudent);
		$this-&amp;gt;IdStudent = $this-&amp;gt;insert_bdd();
	}
	
	public function update()
	{
		$this-&amp;gt;NameStudent = strtoupper($this-&amp;gt;NameStudent);
		$this-&amp;gt;update_bdd();
	}
	
	public function delete()
	{
		$this-&amp;gt;delete_bdd();
	}
	
	//------------------------------
	
	public static function initByPrimaryKey($Pk)
	{
		$c = __CLASS__;
		$p = new $c;
		$p-&amp;gt;init_by_primaryKey($Pk);
		return $p;
	}
	
	public static function deleteDirectly($Pk)
	{
		parent::deleteDirectly(self::$_table, self::$_primaryKey, $Pk);
	}
	
	public static function getAll()
	{
		return parent::getAll(__CLASS__, self::$_table);
	}
	
	public static function getCount()
	{
		return parent::getCount(self::$_table);
	}
}

?&amp;gt;

&amp;lt;?php

include_once('class_mySQL.php');
include_once('class_student.php');

//-------------------------

$p = new student;
$p-&amp;gt;NameStudent = 'DUPONT';
$p-&amp;gt;AgeStudent = 17;
$p-&amp;gt;PictureStudent = 'pic124.jpg';
$p-&amp;gt;insert(); // INSERT DEMO
print_r($p); // id automatically set
echo '&amp;lt;br /&amp;gt;';

$p-&amp;gt;AgeStudent++;
$p-&amp;gt;update(); // UPDATE DEMO

//-------------------------

$q = new student; // don't re-connect ;)
$q-&amp;gt;NameStudent = 'test'; // will be put in capital letters
$q-&amp;gt;AgeStudent = 20;
$q-&amp;gt;PictureStudent = 'pic125.jpg';
$q-&amp;gt;insert();

$u = array('IdStudent' =&amp;gt; $q-&amp;gt;IdStudent); // primary key of $q
$r = student::initByPrimaryKey($u); // initByPrimaryKey DEMO
print_r($r);
echo '&amp;lt;br /&amp;gt;';

//-------------------------

$n = student::getCount(); // getCount DEMO
echo 'We have '.$n.' students in database&amp;lt;br /&amp;gt;';

$students = student::getAll(); // getAll DEMO
print_r($students);
echo '&amp;lt;br /&amp;gt;';

//-------------------------

$p-&amp;gt;delete(); // DELETE DEMO
student::deleteDirectly($u); // STATIC DELETE DEMO

//-------------------------

//$p-&amp;gt;db_connected = 'lol'; // not possible (static member, not object member)
//$p-&amp;gt;db_host = 'ahah'; // not possible
//echo $p-&amp;gt;db_host; // not possible

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/416-mysql-original-php-5-wrapper-class" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14324</id>
    <published>2008-08-01T14:40:14+00:00</published>
    <title>[PHP] On objects that deliver formatted HTML</title>
    <content type="html">

&lt;pre&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;
&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; lang=&amp;quot;fr&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv=&amp;quot;Content-Type&amp;quot; content=&amp;quot;text/html; charset=iso-8859-1&amp;quot; /&amp;gt;
&amp;lt;title&amp;gt;Demo 1&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;b&amp;gt;ID :&amp;lt;/b&amp;gt; [var.guy.IdStudent]&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;NAME :&amp;lt;/b&amp;gt; [var.guy.NameStudent]&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;AGE :&amp;lt;/b&amp;gt; [var.guy.AgeStudent]

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/415-objects-that-deliver-formatted-html/refactors/14324" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14322</id>
    <published>2008-08-01T14:39:47+00:00</published>
    <title>[PHP] On objects that deliver formatted HTML</title>
    <content type="html">&lt;p&gt;Everything is not in the class user : your controller (what you call &amp;quot;the rendering page&amp;quot;) is outside !&lt;/p&gt;

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

&lt;p&gt;I recommend you a template system like TinyButStrong for example : &lt;a href="http://www.tinybutstrong.com/" target="_blank"&gt;http://www.tinybutstrong.com/&lt;/a&gt;
&lt;br /&gt;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 :)&lt;/p&gt;

&lt;p&gt;To give you a simple example i joined the first demo.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php 

class student 
{ 
    public $IdStudent; 
    public $NameStudent; 
    public $AgeStudent; 
} 

$guy = new student(); 
$guy-&amp;gt;IdStudent = 5; 
$guy-&amp;gt;NameStudent = 'WOOD'; 
$guy-&amp;gt;AgeStudent = 22; 

include_once('includes/tbs_class_php5.php'); 
$TBS = new clsTinyButStrong; 
$TBS-&amp;gt;LoadTemplate('demo1.html'); 
$TBS-&amp;gt;Show(); 

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>TiTi</name>
      <email>anthibug@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/415-objects-that-deliver-formatted-html/refactors/14322" rel="alternate"/>
  </entry>
</feed>
