28

Can anyone tell the difference between mysqli->commit and mysqli::commit?

The header in this page is mysqli::commit, but in examples they use mysqli->commit.

nbro
  • 15,395
  • 32
  • 113
  • 196

9 Answers9

68

-> is used when referring to a member of an object.

:: is the Scope Resolution Operator and is used to refer to a static member of a Class.

Consider the following class:

class FooBar {
    public static function fizz() {
        echo "Fizz";
    }

    public function buzz() {
        echo "Buzz";
    }
}

You would call the function buzz() using ->:

$myFooBar = new FooBar();
$myFooBar->buzz();

But would use :: to call the functon fizz(), as it is a static member (a member which doesn't require an instance of the class to be called):

FooBar::fizz();

Also, while we are talking about the difference between static members versus instantiated members, you cannot use $this to refer to the current instance within static members. You use self instead (no leading $) which refers to the current class, or parent if you want to refer to the parent class, or if you have the pleasure of working with PHP 5.3.0, static (which allows for late static binding).


The documentation uses :: to refer to a function inside a class as the class name in the header is not an instance of the class. Still using the same example, a documentation entry referring to the function buzz() would use the following header:

FooBar::buzz

But unless the documentation specifies it's a static member, you will need to use -> on an instance to call it:

$myFooBar = new FooBar();
$myFooBar->buzz();
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
  • 5
    Was about to post, but this is pretty spot on. In short answer to John there is no difference. As Andrew states its a convention in the PHP manual to describe a class method as ClassName::methodName The intention is to use is as an object, i.e. $name->commit() unless it says otherwise in the documentation. – simonrjones Aug 07 '09 at 14:48
  • This answer is amazing. how to do FooBar::fizz()->select("something")->where("something"); – TheCrazyProfessor Jan 03 '17 at 09:18
5

:: is used for static methods.

-> is used for method of an object if you already have the instance.

nbro
  • 15,395
  • 32
  • 113
  • 196
easement
  • 6,119
  • 3
  • 29
  • 36
4

If you have an instance of an object, you use -> to refer to a method inside this instance:

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

Using :: calls a static method without having to create an instance of the object:

Foo::bar();

A static method cannot refer to an it's current instance through $this, but can refer to itself (current class) by using self.

talha2k
  • 24,937
  • 4
  • 62
  • 81
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Calling a non-static member using the scope resolution operator raises an E_STRICT in PHP < 5.3.0 and an E_WARNING in PHP >= 5.3.0. – Andrew Moore Aug 07 '09 at 14:47
  • 1
    And `self` exists to refer to the current class in a static context. It doesn't refer to an instance, but to itself: `class A { static function b() { self::c(); } private static function c() { echo 'hello'; } };` – Andrew Moore Aug 07 '09 at 14:54
1

:: specifies a static (class) method, which is callable without actually instantiating an object. -> specifies an instance (object) method, for which you need an object instantiated to be able to use.

So for example, if you had a variable $m which was an instance of class mysqli, you would call commit by saying $m->commit(), or you could call commit statically by saying MySQLi::commit()

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
0

:: accesses the class' function without instancing an object.

Matt
  • 1,222
  • 1
  • 9
  • 18
0

The -> operator is for object properties.

The :: operator is for class properties.

nbro
  • 15,395
  • 32
  • 113
  • 196
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

in mysqli->commit, mysqli is a instance of MySQLi in mysqli::commit call a static method

andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

mysqli->commit is a public function and mysqli::commit is a static function the two are php object notations of mysqli class.

Houssem
  • 169
  • 1
  • 1
  • 8
0

usually in php.net documentation :: means that this class has that method. For pratical usage you must follow the example so use the -> sintax.

mck89
  • 294
  • 2
  • 5