PHP 4 introduced basic OOP to PHP, however it had a few key issues which were fixed in PHP 5.
Bear in mind that OOP is a concept which has feature which should be consistent between the many different programming languages which implement it.
In most programming languages, an object can be passed to functions or other objects by reference, not by value. In other words, rather than passing a new object with the same values, the actual object is passed, meaning changes are kept.
Here's an example of how it is in PHP 5:
PHP Code:
class Person{
public $Name;
}
function Something(Person $Person){
$Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something($A);
echo $A->Name; //Jimbob
Though, in PHP 4, the same code (IIRC) would output 'James', as the function recieves only a copy of $A.
PHP Code:
class Person{
var $Name;
}
function Something($Person){
$Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something($A);
echo $A->Name; //James
Because of this, you had to manually pass the object by reference rather than by value:
PHP Code:
class Person{
var $Name;
}
function Something($Person){
$Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something(&$A);
echo $A->Name; //Jimbob
However, if you do that in PHP5 you're trying to send a reference by reference, which doesn't really make much sense.
Bookmarks