0
0
PHPprogramming~5 mins

Constants in classes in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a constant in a PHP class?
A constant in a PHP class is a fixed value that cannot be changed once set. It is defined using the <code>const</code> keyword and is accessed without creating an object.
Click to reveal answer
beginner
How do you define a constant inside a PHP class?
Use the <code>const</code> keyword followed by the constant name and value. For example: <code>const PI = 3.14;</code>
Click to reveal answer
beginner
How do you access a class constant from outside the class?
Use the class name followed by the scope resolution operator <code>::</code> and the constant name. For example: <code>ClassName::CONSTANT_NAME</code>
Click to reveal answer
beginner
Can class constants be changed after they are defined?
No, class constants cannot be changed after they are defined. They are fixed values throughout the program.
Click to reveal answer
intermediate
What is the difference between a class constant and a class property?
A class constant is a fixed value defined with <code>const</code> and cannot change. A class property is a variable that can hold different values and can be changed.
Click to reveal answer
How do you define a constant inside a PHP class?
AUsing the <code>var</code> keyword
BUsing the <code>const</code> keyword
CUsing the <code>static</code> keyword
DUsing the <code>define()</code> function
How do you access a class constant from outside the class?
AUsing <code>ClassName::CONSTANT</code>
BUsing <code>$object->CONSTANT</code>
CUsing <code>$object::CONSTANT</code>
DUsing <code>ClassName->CONSTANT</code>
Can you change the value of a class constant after it is set?
AYes, anytime
BOnly inside the class constructor
CNo, constants are immutable
DOnly if declared as <code>static</code>
Which keyword is NOT used to define a class constant?
Avar
Bconst
Cdefine
Dstatic
What is the correct way to define a constant named VERSION with value '1.0' inside a class?
Adefine('VERSION', '1.0');
Bstatic $VERSION = '1.0';
Cvar VERSION = '1.0';
Dconst VERSION = '1.0';
Explain how to define and access a constant inside a PHP class.
Think about how constants are fixed and how you call them without creating an object.
You got /3 concepts.
    Describe the difference between a class constant and a class property in PHP.
    Consider mutability and how you use each in a class.
    You got /4 concepts.