Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the "::" notation in php used for?

Tags:

php

I am looking through some php code and I see this "::" notation that i have no idea what it means...also what the & in the front of the call

$mainframe =& JFactory::getApplication('site');
$sql="SELECT rt.member_id ,rt.commission,rt.sales,kt.store_id,kt.user_id FROM jos_report
rt JOIN jos_kingdom_tickets kt WHERE rt.member_id=kt.ticket_id";
$db =& JFactory::getDBO();

thanks in advance

like image 605
Matt Elhotiby Avatar asked Jun 11 '11 03:06

Matt Elhotiby


People also ask

What does the :: mean in PHP?

In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.

What does :: class mean in PHP?

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

What is the difference between :: and -> in PHP?

They are for different function types. -> is always used on an object for static and non-static methods (though I don't think it's good practice use -> for static methods). :: is only used for static methods and can be used on objects (as of PHP 5.3) and more importantly classes.

What is the use of the symbol in PHP?

The at sign (@) is used as error control operator in PHP. When an expression is prepended with the @ sign, error messages that might be generated by that expression will be ignored. If the track_errors feature is enabled, an error message generated by the expression and it will be saved in the variable $php_errormsg.


4 Answers

::, the scope resolution operator, is used for referencing static members and constants of a class. It is also used to reference a superclass's constructor. Here is some code illustrating several different uses of the scope resolution operator:

<?php
class A {
    const BAR = 1;
    public static $foo = 2;
    private $silly;

    public function __construct() {
         $this->silly = self::BAR;
    }
}

class B extends A {
    public function __construct() {
        parent::__construct();
    }

    public static function getStuff() {
         return 'this is tiring stuff.';
    }
}

echo A::BAR;
echo A::$foo;
echo B::getStuff();
?>

A little trivia: The scope resolution operator is also called "paamayim nekudotayim", which means "two dots twice" in hebrew.

& in the context of your example isn't doing anything useful if you are using php 5 or greater and should be removed. In php 4, this used to be necessary in order to make sure a copy of the returned object wasn't being used. In php 5 object copies are not created unless clone is called. And so & is not needed. There is still one case where & is still useful in php 5: When you are iterating over the elements of an array and modifying the values, you must use the & operator to affect the elements of the array.

like image 133
Asaph Avatar answered Oct 27 '22 07:10

Asaph


You can use it to reference static methods from a class without having to instantiate it.

For example:

class myClass {

    public static function staticFunction(){
        //...
    }

    public function otherFunction(){
        //...
    }

}

Here you could use myClass::staticFunction() outside of the class, but you would have to create a new myClass object before using otherFunction() in the same way.

like image 29
Eric Yang Avatar answered Oct 27 '22 08:10

Eric Yang


It's the scope operator, used for referencing constants or static methods under classes. So:

class C {
    const D = 2;
}

echo C::D; // 2

In your case, it calls a method of the class not tied to a particular instance.

like image 23
Ry- Avatar answered Oct 27 '22 06:10

Ry-


:: is the scope operator in PHP, c++, but not in Java. In this case, it is used to call a static method of a class. A static method is a method which can be called from outside the class, even when you don't have an instance of it.

& indicates that rather than making a copy of what the function returns, it takes the reference to the object returned. In this case, they seem to return singleton objects which are used in the application, e.g. to interface with the database (in the second case)

like image 35
Cronco Avatar answered Oct 27 '22 08:10

Cronco