What does :: mean in php
0 6732
In PHP, this symbol is called Scope Resolution Operator or in general way Double Colon. As name suggested, it is used to make reference to the scope of classes, objects of classes and namespaces.
With the help of Scope resolution operator we can access the methods and properties or variables of a particular class outside the class.
Note: Most of the time we use this operator to access the static variables and methods of a class outside the class.
Example 1:
<?php
class Fruits{
public $fruit_name="apple";
function getFruits() {
echo "Welcome to Coding Tag";
}
}
$result = Fruits::getFruits(); // scope resolution operator is used with class name
echo $result;
?>Output:

Example 2:
<?php
class Fruits{
public static $fruit1="apple"; // static variable
function getFruit($fruit="orange"){
echo "Local variable value : ".$fruit."<br>"; // print local property
echo "Static variable value : ".self::$fruit1; // print static property
}
}
$result = Fruits::getFruit(); /// scope resolution operator is used with class name
echo $result;
?>Output:

Share:




Comments
Waiting for your comments