tags:

views:

165

answers:

8

Possible Duplicate:
PHP: self vs this

I'm working on some code and the previous author seems to intermingle the two.

example:

class DB {
    // Some functions
}

Outside the class he references it like so:

DB::somefunction();

and also like this:

$db = new DB;
$db->somefunction();

What's the difference between the two and is one better than the other?

+11  A: 

:: is for accessing attributes on a class, -> is for accessing attributes on an instance.

Ignacio Vazquez-Abrams
+7  A: 

See http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

The Scope Resolution Operator :: (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

If you have to ask this though, I suggest reading through the chapter on OOP in the PHP manual before continue working on the code at hand. It's basic knowledge.

Gordon
Unexpected Paamayim Nekudotayim is always a fun error the first time you see it.
Austin Fitzpatrick
A: 

:: In this instance means its a static method. A static method can be called without an instance of the class in question, whereas -> accesses a method on an objec instance.

prodigitalson
+3  A: 

:: is for accessing static class functions and members where as -> is for accessing non-static members and functions.

They can be used interchangeably when calling methods, however, it does give a warning if all warnings are enabled (E_STRICT). When you call a non-static method using ::, $this will not be passed to the function (meaning you cannot access any of the objects non-static members).

Example code:

class Foo{
    public function Bar(){
        if ( !isset($this) ){
            echo "Baz";
        }
    }
}

Foo::Bar();

Result:

Baz

For these reasons you should never access a non static function using :: as you will encounter errors if you ever decided to use $this in the function (and immense confusion from the next maintainer).

Yacoby
Why the downvote? I explained why it was possible to call a method using `::` or `->` interchangeably, what the differences were when you used `::` with a non static member and why it shouldn't be done.
Yacoby
:: and -> are not interchangable, a method has to be declared static to be used statically. A warning is generated in PHP 5 if a non static method is called statically even though it might work - might not tho if $this is required. I think PHP 6 will throw a fatal error by the looks of it
meouw
I undownvoted you since you explained more fully :)
meouw
@meouw I added the version to clarify. No warning is given with PHP 5.2.4 on my system. I do not have PHP6 installed to check if this remains the case but the last paragraph covers that case.
Yacoby
You have to enable `error_reporting` with `E_STRICT` to get *Strict Standards: Non-static method Foo::Bar() should not be called statically*
Gordon
+7  A: 

When you call

DB::somefunction();

$this is undefined (NULL) inside somefunction(). So these functions are supposed to not use any instance data. This is accessing class variables/functions.

When you call

$db = new DB;
$db->somefunction();

$this is defined inside somefunction(). These functions are supposed to use instance data. This is accessing instance variables/functions.

parrot:~ more a.php
<?php
class DB {
        var $a = 45;
        function somefunction() {
                var_dump($this);
        }
}

DB::somefunction();
$db = new DB;
$db->somefunction();
?>
parrot:~ php a.php
NULL
object(DB)#1 (1) {
  ["a"]=>
  int(45)
}
Vinko Vrsalovic
+1  A: 
  • :: for static attribute
  • -> instance attribute
Gregoire
+2  A: 

:: is for static invocation. Write Class::Method()

-> is for instance invocation. Write Object->Method()

For the second you first need to instanciate a class, further there is a $this to access instance variables.

ZeissS
A: 

This is in addition to the answers already given i.e. :: static access -> instance access
You will quite often see :: used in documentation when a method of a class is being referred to even though it is not static just for convenience. You would still use -> if you are working with an instance of the class.

meouw