PHP5 is already there for some time but hosting providers and others don't upgrade for several reasons. One of the main reasons is incompatible scripts so in this topic i will write some things once in a while which might be usefull to make your scripts compatible, and some tips and tricks which will ONLY work in PHP5.
To start of, PHP5 has more restrictions on class handling and a lot of new features inside classes.
### CLASS ISSUES ###
var isn't allowed
PHP Code:
class var_test
{
var $bob; # fails
}
Instead just strip
var or if you write your script PHP5 only use one of the following
PHP Code:
class var_test
{
public $bob; # item can be accessed everywhere
protected $bob; # limits access to inherited classes and itself
private $bob; # limits visibility only to the class that defines the item
}
Another thing that isn't allowed is instance reassigning like
PHP Code:
class assign_test
{
public function assign()
{
$this = new Class(); # dies immediately
}
}
### CLASS TIPS ###
Normally you can access inside a class $this if it's a instance
PHP Code:
class this_test
{
public function echo()
{
$this->foo();
}
public function echo_dyn()
{
this_test::foo();
}
public function foo()
{
echo 'foo';
}
}
$test = new this_test();
$test->echo(); # outputs foo
this_test::echo() # fails
this_test::echo_dyn() # outputs foo
The above script is messy when you are extending classes
PHP Code:
class this_test
{
public function echo()
{
$this->foo();
}
public function foo()
{
echo 'foo';
}
}
class this_test2 extends this_test
{
public function echo()
{
this_test::foo();
}
}
$test = new this_test2();
$test->echo(); # outputs foo
this_test2::echo() # outputs foo
This way it overwrites echo() but is pretty nasty in very advanced scripts.
So PHP5 gives additional features like self:: and parent:: which i will explain.
self::
With this you call a function inside the class itself and not thru extension or anything. This means if you overwrite a function it will not succeed with the self operator.
PHP Code:
class this_test
{
public function echo()
{
self::foo();
}
public function foo()
{
echo 'foo';
}
}
class this_test2 extends this_test
{
public function foo()
{
echo 'bar';
}
}
this_test2::echo(); # outputs foo instead of bar
this_test2::foo(); # outputs bar
parent::
With this you call a function in the parent of the class. This means that if you overwrite a function you have still access to the original function. This is extremely usefull if you want to add functionality to a function.
PHP Code:
class this_test
{
public function foo()
{
echo 'foo';
}
}
class this_test2 extends this_test
{
public function foo()
{
parent::foo()
echo 'bar';
}
}
this_test::foo(); # outputs foo
this_test2::foo(); # outputs foo and bar
If you like all this info i will keep adding stuff here once in a while
