|
Posted by Rami Elomaa on 03/23/07 15:44
Anthony Smith kirjoitti:
> I can call a class using "->", but it complains about the ::
> I see on the net where :: is used. Is there a good explanation on when
> to use one over the other or the differences?
>
> $help = new help();
> $help->foo();
> $help::foo();
>
>
> class help{
>
> function foo(){
> echo "test";
> }
> }
>
:: is used for calling a method by class, not object. This is the part
where php becomes rocket science. You can call a method of a class
without having created an instance of the class eq. an object by calling
it via the class.
So if you have
class help {
function foo(){
echo "Hello kitty!";
}
}
You can call the method foo without creating an object:
help::foo();
Or you can create an instance of the class:
$myhelp = new help();
and then use the class pointer to call the method
$myhelp->foo();
Why use :: instead of -> then? hmm... I dunno, it's kewl? I've only used
it once with a singleton where it seemed to make sense buy it's actually
quite rare I think. People who work with objects big-time may have use
for it, but I don't think it's actually all that necessary. It's good to
have and good to know what it is and what it does, but on most days
you'll be fine without it.
--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
Navigation:
[Reply to this message]
|