468,720 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

php classes calling functions using :: and ->

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";
}
}

Mar 23 '07 #1
6 1721
>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 to call class methods or properties when the class has not been
initiated:-

http://www.php.net/manual/en/keyword...ekudotayim.php
Mar 23 '07 #2
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.

--
Ra*********@gmail.com
"Olemme apinoiden planeetalla."
Mar 23 '07 #3
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.
I think it is done to lower the overheads if for example you only need to
use 1 function within the class. Instead of having the overhead of
instantiating the class then calling the method (which I presume allocates
memory for the methods and properties).
Mar 23 '07 #4
On Mar 23, 11:44 am, Rami Elomaa <rami.elo...@gmail.comwrote:
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.Elo...@gmail.com
"Olemme apinoiden planeetalla."

It's really not all that rare -- in fact it's quite common.

As a matter of form, you should define class methods as static and
call them with :: If you get into the habit of calling your instance
methods with :: you can run into problems because $this is not
available (and PHP will complain accordingly). Furthermore there are
differences in inheritance between static methods and instance
methods -- namely that there is no inheritance with static methods --
but that provides you with a certain performance benefit.

The short answer (and it's certainly not the right one in all cases)
is that if you use $this in your method, make it an instance method
and call it with ->. If you don't use $this in your method, declare
it static and call it with ::

Mar 23 '07 #5
peter schreef:
>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.

I think it is done to lower the overheads if for example you only need to
use 1 function within the class. Instead of having the overhead of
instantiating the class then calling the method (which I presume allocates
memory for the methods and properties).
imho it would also be bad design / sort of misleading to create an
object you only use for calling a function on that does not need any
information from the object itself. With php5 you can also declare the
function static, so that when the code is maintained (maybe years later
by another programmer) it won't get modified to expect the object to be
properly initialized.

To say it an other way, making a static function and using :: will
communicate your intentions more clearly in your source code.

(i don't think the methods need extra memory for each extra object you
instantiate. And php only adds properties that are either declared or
actually assigned. But the object itself will probably be allocated, and
deallocated when it is no longer referenced, so i think you are right
about the overhead)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.
Mar 23 '07 #6
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!
Mar 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by David MacQuigg | last post: by
49 posts views Thread by Christopher J. Bottaro | last post: by
45 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by Patrick Kowalzick | last post: by
12 posts views Thread by Daedalus.OS | last post: by
6 posts views Thread by Alden Pierre | last post: by
47 posts views Thread by Larry Smith | last post: by
6 posts views Thread by Miguel Guedes | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.