A

Admin • 833K Points
Coach

Q. From the following is the right way to call a class constant, given that the class is mathFunction?

  • (A) echo PI;
  • (B) echo mathFunction::PI;
  • (C) echo mathFunction->PI;
  • (D) echo mathFunction=PI;
  • Correct Answer - Option(B)
  • Views: 22
  • Filed under category PHP
  • Hashtags:

Explanation by: Admin

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

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.