473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1945
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
49
by: Christopher J. Bottaro | last post by:
I find myself doing the following very often: class Struct: pass .... blah = Struct() blah.some_field = x blah.other_field = y ....
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
7
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.