tags:

views:

84

answers:

3

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

+2  A: 

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.

Jonathan Sampson
guess its ok if we dont write public as far as I know there is nothing like public private access types in PHP?
Josh
Yes, Nishant. There are public, private, protected in OOP in PHP.
Jonathan Sampson
Visiblity: http://www.php.net/manual/en/language.oop5.visibility.php
Jonathan Sampson
Better yet- Nishant read all of this: http://php.net/manual/en/language.oop5.php
Mike B
+2  A: 

It passes the variable as a reference but this is no longer needed in PHP5+.

Brad
A: 

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.

Gordon
I recently started learning PHP.
Josh
You are probably learning from outdated material then. PHP4 is dead. Have a look at the chapters *Getting Started* and *Language Reference* in the PHP manual. The manual is actually pretty good.
Gordon