note 61353 added to language.oop5.basic
| From: | benATchemica dot co dot uk at osu1 dot php dot net | Date: | Wed, 01 Feb 2006 13:40:36 +0000 |
| Subject: | note 61353 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102810@lists.php.net to get a copy of this message | ||
PHP classes helpfully allow you to define member variables on the fly. On top of that, dynamic
variables mean you can create them by name from strings.
This means you can ape the 'Ruby On Rails' view system - or replace heavyweight templating
systems like Smarty - with very little code:
view.class.php:
<?
class View
{
public function AddVariables($aVariables){
foreach($aVariables as $name => $value){
$this->AddVariable($name, $value);
}
}
public function AddVariable($name, $value){
$this->$name = $value; // <--- Note the extra $
}
public function Render($template){
include($template . ".php");
// You could put validation in here, so it exits cleanly if the template is missing.
}
protected function AddParagraph($strText){
echo("<p>" . $strText . "</p>");
}
protected function AddOptions($aOptions, $strSelected){
foreach($aOptions as $value => $text){
echo "<option value='" . $value . "'";
if($value==$strSelected){echo " selected";}
echo ">" . $text . "</option>\n";
}
}
}
// The code below would normally be in a calling page/class.
$v = new View();
$v->AddVariables(array("peach"=>"fruit",
"carrot"=>"vegetable"));
$v->AddVariables(array("options" => array("1"=>"Jan",
"2"=>"Feb", "3"=>"March")));
$v->AddVariable("bodytext", "Some content goes here");
$v->Render("test");
?>
test.php:
<html>
<body>
<p>A peach is a <? echo $this->peach; ?>.</p>
<p>A carrot is a <? echo $this->carrot; ?>.<p>
<form action="#" method="GET">
<select id="test">
<? $this->AddOptions($this->options, 2); ?>
</select>
</form>
<? $this->AddParagraph($this->bodytext); ?>
</body>
</html>
This means you can unleash the awesome power of PHP on your templates, allowing all kinds of cool
tricks, while ensuring you're not mixing up your display and business code. You only have
access to the variables you pass in there yourself. Oh, and the library of markup rendering member
functions you'll create. In PHP.
You can re-use templates - just use the same variables with a different page.
You can swap in different mark-up languages programatically, so you could make versions that spit
out HTML, XHTML, WML, and XML for SOAP interfaces.
It's entirely extensible. Either write in more PHP member functions to automate view-based
tasks, or derive new classes and extend the functionality that way. (Perfect for your proprietary
XML language helper functions - they really don't belong in the same class that's
generating XHTML.)
The best part is that it's all PHP, so you don't have to learn a new language (as you
would do with Smarty - or if you wanted to switch over to Ruby on Rails.)
Hope someone finds it useful. If anyone makes any improvements, corrections etc. I'd love to
see them. :)
You'll need a recent version of PHP5 to run it - some earlier versions gave strange scoping
errors with the use of $this. I know that build date 'Jan 11 2006 16:35:21' works.
----
Server IP: 84.18.202.180
Probable Submitter: 81.130.202.94
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61353
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61353&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61353&report=yes
Search -- http://master.php.net/manage/user-notes.php