You are here: Re: php classes calling functions using :: and -> « PHP Programming Language « IT news, forums, messages
Re: php classes calling functions using :: and ->

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация