The features I'm looking forward to using are destructor and property. They
allow you to very neatly encapsulate your database access code. Say you're
writing a message forum webapp. You would have a Message class that looks
something like this:
class Message {
private $values;
private $columns_change d;
private $primary_key;
function __destruct() {
if($this->primary_key) {
$this->DoDBUpdate() ;
}
else {
$this->DoDBInsert() ;
}
}
function DoDBUpdate() {
foreach($this->columns_change d as $column) {
...
}
}
function __set($name, $value) {
if($this->values[$name] != $value) {
$this->values[$name] = $value;
$this->columns_change d[] = $name;
}
}
function __get($name) {
return $this->values[$name];
}
}
To insert a message, you'll would do something like:
$msg = new Message();
$msg->title = $_POST['title'];
$msg->text = $_POST['text'];
Assignments to the undeclared variables title and text triggers the objects
__set() function, which records the names of these properties so that it
knows which table columns to update. When the object is destroyed, the
destructor is triggered, which save the changes to the database.
Property access is great in that it fits in perfectly with one of PHP's
great strength: variable interpolation (i.e. sticking variables into
double-quoted strings). You can now, in essence, call a function from within
a string. Using the message forum example again: When you out put a message,
you'll find inevitably that you need to do a htmlspecialchar s() on most
properties of Message. In PHP4, you would have to either use a lot of
temporary variables or break up your strings into ugly fragments ( "<h1>" .
htmlspecialchar s($msg->title) . "</h1>" ). In PHP5, you can modify __get()
like this:
function __get($name) {
if(substr($name , -5) == '_html')) {
$need_htmlspeci alchars = true;
$name = substr($name, 0, -5);
}
$value = $this->values[$name];
if($need_htmlsp ecialchars) {
$value = htmlspecialchar s($value);
}
return $value;
}
so that all you have to do is to append the suffix '_html' to a property
name to get it properly escaped ( echo "<h1>$msg->title_html</h1>" ).
Uzytkownik "mikel" <mi****@komatia .com> napisal w wiadomosci
news:e9******** *************** ***@posting.goo gle.com...
Hello,
php5 is in everybodies mouth - but what are the *real* advantages and
plus' compared to php4?
mikel