Q. From the following is the right way to call a class constant, given that the class is mathFunction?
β
Correct Answer: (B)
echo mathFunction::PI;
Explanation: For calling any constant we use double colon (::).
Syntax for calling constant outside the class.
For creating constant we use const.
className::constantName;
Example:
class mathFunction{
const PI = 3.14;
}
//Now we are calling PI outside the class so
echo mathFunction::PI; //returns 3.14
Case 2: we want to call constant inside the class then
class mathFunction{
const PI = 3.14;
function get_pi_value{
echo SELF::PI;
}
}
//Create object
$obj = new mathFunction ();
$obj->get_pi_value(); //returns 3.14