Explore the essentials of Object-Oriented Programming in PHP with this quiz, covering key concepts like classes, objects, properties, methods, and interfaces. Sharpen your understanding of PHP OOP principles to write cleaner and more modular code.
Which keyword is used to define a class in PHP when creating a blueprint for an object?
Explanation: The correct keyword to define a class in PHP is 'class'. 'object' refers to an instance of a class, not how it is defined. 'define' is used for constants, and 'structure' is not a valid PHP keyword. Using 'class' lets you group properties and methods logically.
How do you create a new object named $vehicle from a class named Car in PHP?
Explanation: You create an object from a class by using the 'new' keyword followed by the class name and parentheses. Options like '$vehicle = create Car();' or '$vehicle = Car-u003Enew();' do not follow correct PHP syntax. '$vehicle = new object Car();' is not valid syntax in PHP either.
If a class User has a public property $name, how do you access this property from an object $user?
Explanation: In PHP, you access object properties using the '-u003E' operator as in '$user-u003Ename'. '$user.name' and 'user-u003Ename' are invalid because PHP does not use dot notation for objects and needs the variable symbol. '$user['name']' is used for arrays, not objects.
What is the purpose of a constructor method like __construct() in a PHP class?
Explanation: A constructor method, typically called __construct(), is used to initialize or set up properties when an object is created. It is not used for destroying objects; that is the destructor's role. Static methods are unrelated to object construction, and constants have their own definition mechanism.
In PHP, which keyword is used in a class declaration to indicate it is following an interface’s contract?
Explanation: The correct syntax to indicate a class uses an interface is the 'implements' keyword. 'extends' is used for inheriting from another class. 'includes' and 'inherits' are not PHP keywords for OOP. Implementing an interface ensures the class has all the interface's methods.
Which visibility keyword restricts access to a property so it can only be used within its own PHP class?
Explanation: The 'private' keyword limits a property’s access to just the class where it is defined. 'public' makes it accessible from outside the class. 'static' relates to class-level properties and does not affect visibility. 'open' is not a keyword in PHP for property visibility.
How do you call a static method sayHello from the class Greetings without instantiating an object?
Explanation: A static method is called with the double colon '::', as in 'Greetings::sayHello();'. 'Greetings-u003EsayHello();' and 'Greetings.sayHello();' use incorrect syntax; the arrow is for objects, and dot notation isn't used in PHP. Instantiating the class is unnecessary for static methods.
Which statement best describes a PHP interface?
Explanation: A PHP interface only defines method signatures that classes must implement, but does not provide any method code. It does not manage inheritance of properties, store static data specifically, or create objects automatically. Interfaces focus on establishing a contract for classes.
Given: class Demo {}; $obj = new Demo(); What type of variable is $obj after this code runs?
Explanation: After instantiation, $obj is an object of the class Demo. It is not a string or integer, as object creation does not result in those types. An interface is a contract, not an instance, and cannot be used like an object.
How would you call the method getData() of an object $item?
Explanation: You call an object's method in PHP with the '-u003E' operator, as shown in '$item-u003EgetData();'. Using dot notation or colon is incorrect in PHP syntax, and '$item-u003Eget_Data();' is only correct if the method is specifically named 'get_Data'.