PHP - Classes and ObjectsWhich of the following is the correct way to declare a constructor method in a PHP class?Afunction construct() {}Bfunction __construct() {}Cfunction _construct() {}Dfunction __destruct() {}Check Answer
Step-by-Step SolutionSolution:Step 1: Recall PHP constructor syntaxPHP uses the magic method named __construct() as the constructor.Step 2: Compare options to correct syntaxOnly function __construct() {} matches the exact method name with double underscores.Final Answer:function __construct() {} -> Option BQuick Check:Constructor syntax = __construct() [OK]Quick Trick: Constructor always named __construct() with two underscores [OK]Common Mistakes:Using single underscore instead of doubleConfusing constructor with destructor __destruct()Omitting underscores entirely
Master "Classes and Objects" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Classes and Objects - Properties and visibility - Quiz 3easy Classes and Objects - Class declaration syntax - Quiz 4medium Classes and Objects - Access modifiers (public, private, protected) - Quiz 3easy File Handling - Why file operations matter - Quiz 15hard Inheritance and Polymorphism - Constructor inheritance - Quiz 13medium Sessions and Cookies - Why state management is needed - Quiz 5medium String Functions - Why string functions matter - Quiz 15hard String Functions - Implode and join - Quiz 4medium Superglobals and Web Context - Input validation and sanitization - Quiz 6medium Superglobals and Web Context - $_REQUEST behavior - Quiz 10hard