|
Posted by Toby A Inkster on 03/25/07 20:39
Anthony Smith wrote:
> I can call a class using "->", but it complains about the ::
The difference between them is much the same as the difference between an
object and a class.
class Person
{
public $firstname;
public $surname;
public $spouse;
public function __construct ($firstname, $surname)
{
$this->firstname = $firstname;
$this->surname = $surname;
echo $this->get_name()." was born.\n";
}
public function get_name ()
{
return sprintf("%s %s",
$this->firstname,
$this->surname);
}
public function introduce_myself ()
{
return "My name is ".$this->get_name()."\n";
}
public static function wedding ($a, $b, $changename=FALSE)
{
if (!empty($a->spouse) || !empty($b->spouse))
return FALSE;
$a->spouse = $b;
$b->spouse = $a;
printf("%s and %s are married!\n",
$a->get_name(),
$b->get_name());
if ($changename)
$b->surname = $a->surname;
return TRUE;
}
}
$d = new Person('Dave', 'Jones');
$m = new Person('Mary', 'Smith');
$d->introduce_myself();
$m->introduce_myself();
Person::wedding($d, $m, TRUE);
$m->introduce_myself();
The -> operator operates on objects, the :: operator operates on classes.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
Navigation:
[Reply to this message]
|