Bird
0
0

What is wrong with this PHP code?

medium📝 Debug Q14 of 15
PHP - Classes and Objects
What is wrong with this PHP code?
class Book {
    const TITLE = 'PHP Basics';
}

$book = new Book();
echo $book::TITLE;
ACannot access constant via object instance with ::
BConstant TITLE is not declared correctly
CMissing semicolon after constant declaration
DCode will output 'PHP Basics' without error
Step-by-Step Solution
Solution:
  1. Step 1: Verify constant declaration

    The class Book correctly declares const TITLE = 'PHP Basics';.
  2. Step 2: Verify access syntax

    $book = new Book(); creates an instance, and $book::TITLE is valid syntax to access the class constant from an instance. The code outputs PHP Basics without error.
  3. Final Answer:

    Code will output 'PHP Basics' without error -> Option D
  4. Quick Check:

    $instance::CONSTANT is valid [OK]
Quick Trick: ClassName::CONSTANT or $instance::CONSTANT both valid [OK]
Common Mistakes:
  • Using -> for constants instead of ::
  • Declaring with var instead of const
  • Thinking $instance::CONSTANT is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes