please help me with the my OOP PHP doubt?
I have the foll prog
class Person {
var $name;
function Person () {
}
}
$fred = new Person;
$fred->name = "Fred";
$barney =& new Person;
$barney->name = "Barney";
echo $barney->name;
echo $fred->name;
both the echo statements give the right same output ie "Fred" and "Barney" so whats the use of giving 开发者_如何学编程& while declaring $barney. what does "&" refer to here?
THanks
It passes the variable as a reference but this is no longer needed in PHP5+.
References in PHP4 (http://www.php.net/manual/en/oop4.newref.php)
It means 'by reference.' It becomes synonymous, rather than a copy of (further reading). Also, the new way to write a constructor is not to use the same name as the class itself, instead, you use the __construct()
function:
class Person {
function __construct() {
// setup shop
}
}
Also, be sure to use Visiblity properly by assigning certain methods and properties as 'public,' 'private,' 'protected,' etc.
See References Explained in the PHP Manual.
Is there any particular reason why you are still using PHP4? It is no longer supported and PHP5s OOP is much more solid and capable.
精彩评论