johnnietheblack
johnnietheblack

Reputation: 13310

PHP Classes: when to use :: vs. ->?

I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.

What are the benefits of each, and what is the right situation to use either?

Upvotes: 36

Views: 16415

Answers (7)

popeye
popeye

Reputation: 501

:: is used to access a class static property. And -> is used to access a class instance ( Object's ) property.

Consider this Product class that has two functions for retrieving product details. One function getProductDetails belongs to the instance of a class, while the other getProductDetailsStatic belongs to the class only.

class Product {
  protected $product_id;

  public function __construct($product_id) {
    $this->product_id = $product_id;
  }

  public function getProductDetails() {
     $sql = "select * from products where product_id= $this->product_id ";
     return Database::execute($sql);
  }

  public static function getProductDetailsStatic($product_id) {
     $sql = "select * from products where product_id= $product_id ";
     return Database::execute($sql);
  }
}

Let's Get Products:

$product = new Product('129033'); // passing product id to constructor
var_dump( $product->getProductDetails() ); // would get me product details

var_dump( Product::getProductDetailsStatic('129033') ); // would also get me product details

When to you use Static properties?

Consider this class that may not require a instantiation:

class Helper {
  static function bin2hex($string = '') {
  }
  static function encryptData($data = '') {
  }
  static function string2Url($string = '') {
  }
  static function generateRandomString() {
  }
}

Upvotes: 1

MrHus
MrHus

Reputation: 33378

Php can be confusing in this regard you should read this.

What's also confusing is that you can call non static functions with the :: symbol. This is very strange when you come from Java. And it certainly surprised me when I first saw it.

For example:

class Car
{
    public $name = "Herbie <br/>";

    public function drive()
    {
        echo "driving <br/>";
    }

    public static function gas()
    {
        echo "pedal to the metal<br/>";
    }
} 

Car::drive(); //will work
Car::gas(); //will work

$car = new Car();
$car->drive(); // will work
$car->gas(); //will work

echo $car->name; // will work
echo Car::$name; // wont work error

As you can see static is very loose in php. And you can call any function with both the -> and the :: symbols. But there is a difference when you call with :: there is no $this reference to an instance. See example #1 in the manual.

Upvotes: 7

Andr&#233; Hoffmann
Andr&#233; Hoffmann

Reputation: 3555

It should also be noted that every static function can also be called using an instance of the class but not the other way around.

So this works:

class Foo
{
  public static function bar(){}
}

$f = new Foo();
$f->bar(); //works
Foo::bar(); //works

And this doesn't:

class Foo
{
  protected $test="fddf";
  public function bar(){ echo $this->test; }
}

$f = new Foo();
$f->bar(); //works
Foo::bar(); //fails because $this->test can't be accessed from a static call

Of course you should restrict yourself to calling static methods in a static way, because instantiating an instance not only costs memory but also doesn't make much sense.

This explanation was mainly to illustrate why it worked for you some of the times.

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61557

When you declare a class, it is by default 'static'. You can access any method in that class using the :: operator, and in any scope. This means if I create a lib class, I can access it wherever I want and it doesn't need to be globaled:

class lib
{
    static function foo()
    {
       echo "hi";
    }
}
lib::foo(); // prints hi

Now, when you create an instance of this class by using the new keyword, you use -> to access methods and values, because you are referring to that specific instance of the class. You can think of -> as inside of. (Note, you must remove the static keyword) IE:

class lib
    {
        function foo()
        {
           echo "hi";
        }
    }
$class = new lib;
$class->foo(); // I am accessing the foo() method INSIDE of the $class instance of lib.

Upvotes: 5

Peter Bailey
Peter Bailey

Reputation: 105878

Simply put, :: is for class-level properties, and -> is for object-level properties.

If the property belongs to the class, use ::

If the property belongs to an instance of the class, use ->

class Tester
{
  public $foo;
  const BLAH;

  public static function bar(){}
}

$t = new Tester;
$t->foo;
Tester::bar();
Tester::BLAH;

Upvotes: 78

gnarf
gnarf

Reputation: 106332

Sourcing WikiPedia - Class

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint describes the state and behavior that the created objects all share. An object created by a class is an instance of the class, and the class that created that instance can be considered as the type of that object, e.g. a type of an object created by a "Fruit" class would be "Fruit".

The :: operator accesses class methods and properties which are defined in php using the static keyword. Class const are also accessed using ::

The -> operator accesses methods and properties of an Instance of the class.

If the function operates on an instance, you'll be using ->. If it operates on the class itself, you'll be using ::

Another use of :: would be when you want to call your parent functions. If one class inherits another - it can override methods from the parent class, then call them using parent::function()

Upvotes: 0

Cody Caughlan
Cody Caughlan

Reputation: 32748

The "::" symbol is for accessing methods / properties of an object that have been declared with the static keyword, "->" is for accessing the methods / properties of an object that represent instance methods / properties.

Upvotes: 9

Related Questions