Which syntax correctly defines a class named 'Person' in PHP?
easy📝 Syntax Q3 of 15
PHP - Classes and Objects
Which syntax correctly defines a class named 'Person' in PHP?
A<?php new Person { } ?>
B<?php function Person() { } ?>
C<?php class Person { } ?>
D<?php var Person = class() { } ?>
Step-by-Step Solution
Solution:
Step 1: Recall PHP class syntax
In PHP, a class is defined using the keyword 'class' followed by the class name and curly braces.
Step 2: Evaluate each option
<?php class Person { } ?> uses correct syntax. <?php function Person() { } ?> uses 'function' which is wrong. <?php new Person { } ?> uses 'new' incorrectly. <?php var Person = class() { } ?> uses invalid syntax.
Final Answer:
<?php class Person { } ?> -> Option C
Quick Check:
Class syntax = class keyword + name + braces [OK]
Quick Trick:Use 'class ClassName { }' to define a class in PHP [OK]
Common Mistakes:
Using 'function' instead of 'class'
Trying to instantiate with 'new' in definition
Incorrect variable assignment for class
Master "Classes and Objects" in PHP
9 interactive learning modes - each teaches the same concept differently