πŸ“Š PHP
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;
πŸ’¬ Discuss
βœ… 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

Explanation by: Team MCQ Buddy
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

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
154
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Team MCQ Buddy
Publisher
πŸ“ˆ
92%
Success Rate