Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "->" and "::" in PHP MySQLi OOP

People also ask

What's the difference between using Mysql_ functions and PDO?

MySQLi is a replacement for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements. PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases.

What is the difference between MySQLi object-oriented and MySQLi procedural?

MySQLi provides a procedural way, much similar to the MySQL. This is the reason why developers coming from a MySQL background prefers using MySQLi. However, object-oriented programmers prefer PDO because of its compatibility with a large number of databases.

What is better MySQLi or PDO?

The main advantage of PDO over MySQLi is in the database support. PDO supports 12 different database types, in opposition to MySQLi, which supports MySQL only. When you have to switch your project to use another database, PDO makes the process simpler.

What is MySQLi object-oriented?

MySQLi is a new improved extension for accessing mysql database. It allows procedural and object oriented interface for accessing mysql database. Though you can use the old mysql functions but new mysqli offers security, advanced options, speed and similar syntax.


-> 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();

:: is used for static methods.

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


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.


:: 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()