Linked by John Collins on Wed 21st Apr 2004 06:42 UTC
The purpose of this article is to give a novice programmer the basic idea of what OOP is, as implemented using PHP. Readers should have a basic knowledge of programming ie what variables are, variable types, basic methods of writing comments, and how to enter code into a text editor.
Permalink for comment
To read all comments associated with this story, please click here.
It's also important to realise that PHP4 uses value-semantics (as opposed to the more usual reference semantics) for objects. Thus if you say
$bill = new Student("Bill", "Male");
$william = $bill;
three objects will be created. The new command creates an object, and then a copy of this is placed in the variable $bill. On the second line another object is created (a copy of the one in $bill) and is placed in $william.
Thus for the following
$bill = new Student("Bill", "Male");
$william = $bill;
$bill->name = "Bill Gates";
echo $bill->name . "n";
echo $william->name . "n";
You would get
Bill Gates
Bill
This becomes even more of an issue in functions. As a result most PHP programmers explicity pass by reference.
The following code passes by reference
$bill =& new Student("Bill", "Male");
$william =& $bill;
$bill->name = "Bill Gates";
echo $bill->name . "n";
echo $william->name . "n";
and the output is
Bill Gates
Bill Gates
In PHP5 objects will use reference semantics only, and there will be no need for hundreds of & symbols in your code.
It's also important to realise that PHP4 uses value-semantics (as opposed to the more usual reference semantics) for objects. Thus if you say
$bill = new Student("Bill", "Male");
$william = $bill;
three objects will be created. The new command creates an object, and then a copy of this is placed in the variable $bill. On the second line another object is created (a copy of the one in $bill) and is placed in $william.
Thus for the following
$bill = new Student("Bill", "Male");
$william = $bill;
$bill->name = "Bill Gates";
echo $bill->name . "n";
echo $william->name . "n";
You would get
Bill Gates
Bill
This becomes even more of an issue in functions. As a result most PHP programmers explicity pass by reference.
The following code passes by reference
$bill =& new Student("Bill", "Male");
$william =& $bill;
$bill->name = "Bill Gates";
echo $bill->name . "n";
echo $william->name . "n";
and the output is
Bill Gates
Bill Gates
In PHP5 objects will use reference semantics only, and there will be no need for hundreds of & symbols in your code.