-3

I'm looking for quite a time for the answer of this question but Google didn't help so far.

The PHP manual always states i.e. mysqli::prepare but in code I always have to use mysqli->prepare(). Does anybody knows the difference between both types of writing and when it is possible to use which way?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 4
    Static method call vs non-static method call – ka_lin Jan 20 '14 at 09:25
  • …or in other words: [RTM](http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php) – feeela Jan 20 '14 at 09:26
  • Exact duplicate of [What's the difference between :: (double colon) and -> (arrow) in PHP?](http://stackoverflow.com/questions/3173501/whats-the-difference-between-double-colon-and-arrow-in-php) – Rikesh Jan 20 '14 at 09:27
  • Possible duplicate of http://stackoverflow.com/questions/3173501/whats-the-difference-between-double-colon-and-arrow-in-php – rccoros Jan 20 '14 at 09:28

1 Answers1

1

:: is for calling a static method on a class:

Foo::bar();

-> is for calling an instance method on an object of a class:

$foo = new Foo;
$foo->bar();

The two are entirely different.

The reason that the manual always refers to methods in the notation Class::method is that the method is defined as part of a class, and that the notation $obj->method is even more confusing since $obj is a variable of an arbitrary name. It's a convention to refer to methods by their "static" class name to make it unambiguous what method of what class you're talking about. You know whether to call the method statically or on an object instance by the presence or absence of the keyword static in its signature:

public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
       ^^^^^^

http://php.net/manual/en/datetime.createfromformat.php

Static method, call like this:

$foo = DateTime::createFromFormat(...);

But:

public DateTime DateTime::add ( DateInterval $interval )

http://php.net/manual/en/datetime.add.php

Not static, call as instance method:

$foo = new DateTime;
$foo->add(...);
deceze
  • 510,633
  • 85
  • 743
  • 889