hi,
i want to know what are the new features added in php5,i know it is using oops concepts.But,What features were missing in php4 ,now added in php5 makes it better.
thanks...
difference between PHP4 & PHP5
The most important thing that is missing will be continuing upgrades. PHP 4 is deprecated. They just rolled out the last version.
For the most part PHP5 is backwards compatible with PHP4. I did run across an issue with parsing xslt. Other than that, all the scripts written for PHP4 run fine.
anneCAT wrote:But,What features were missing in php4 ,now added in php5 makes it better.
Basically, everything added to PHP in the past four years. New development on PHP 4 stopped when PHP 5 was released. (You can also look at the PHP 5 [man]changelog[/man] and search for the word "Added".)
And now (as bretticus notes), all development of PHP 4 has stopped. It's not merely deprecated, it's obsoleted.
There are a couple of gotchas when using php4 code, like if you're using a global database connection object what also keeps hold of a result resource and do this kind of thing:
$db = new DB('connect:guff');
some_function();
/** does stuff */
function some_function()
{
$db = $_GLOBALS['db'];
$db->query('SELECT some, fields FROM table';
while ($row = $db->fetch())
{
some_other_function($row['id']);
}
}
/** does more stuff */
function some_other_function($id)
{
$db = $_GLOBALS['db'];
$db2 = $db; // make a copy of the database object... fine in php4, but it's not a copy in php5, it's the same thing
$db2->query('SELECT more, things FROM anothertable WHERE id = '.(int)$id);
}
In php 5 that's going to go helluva wrong because $db2 in the second function is a reference to the global $db object, so when some_other_function returns $db in the in the first function will have been modified, screwing up the result set. You have to change $db2 = $db; to $db2 = clone $db; to get the same results. That's the kind of thing that can go wrong. Passing objects to functions also passes them by reference by default, so you also have to be aware of that if using php4 code.
Passing objects to functions also passes them by reference by default
That is not true. Objects are not passed by reference by default. Rather, PHP5 objects are pointers without pointer syntax (or what Java calls references, but that is not the same as what PHP calls references, which are similiar to what C++ calls references). For more information, refer to the PHP manual on Objects and references.
It's a moot point(er). Me joko!
It is not a moot point as it has consequences for upgrading. Consider this PHP4 script:
<?php
class Number {
var $n;
function Number($n = 0) {
$this->n = $n;
}
function value() {
return $this->n;
}
}
function add(&$x, $n) {
$x = new Number($x->value() + $n);
}
$num = new Number();
add($num, 123);
echo $num->value();
?>
When run, it produces the output of 123, as expected.
Now, the server gets upgraded to PHP5, and then the maintainer decides to upgrade the script as well. Due to a failure to understand the concept in question, the maintainer decides to remove the pass by reference since "passing objects to functions also passes them by reference by default":
<?php
class Number {
private $n;
function __construct($n = 0) {
$this->n = $n;
}
function value() {
return $this->n;
}
}
function add(Number $x, $n) {
$x = new Number($x->value() + $n);
}
$num = new Number();
add($num, 123);
echo $num->value();
?>
The output becomes 0, which is incorrect.
I know it's not a moot point, that's why I put me joko. In my own little examples I write as reminders I've got this little chap:
class Foo
{
public $bar = 1;
}
$a = new Foo();
$b = $a;
$b->bar = '$b set me';
echo '<pre>'.print_r($a, 1).'</pre>';
$b = 'soup';
echo '<pre>'.print_r($a, 1).'</pre>';
change($a);
echo '<pre>'.print_r($a, 1).'</pre>';
transform($a);
echo '<pre>'.print_r($a, 1).'</pre>';
function change($obj)
{
// this will change $a outside's bar property
$obj->bar = 'changed by change()';
// below won't change $a outside to a string
$obj = 'I will not really by changed outside';
}
function transform(&$obj)
{
// this will change $a outside's bar property
$obj->bar = 'changed by transform()';
// below WILL change $a outside
$obj = 'really transformed by transform';
}