PHP OOP Fundamentals: Classes, Objects, and Interfaces Quiz Quiz

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.

  1. Defining a Class in PHP

    Which keyword is used to define a class in PHP when creating a blueprint for an object?

    1. object
    2. class
    3. define
    4. structure

    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.

  2. Creating an Object from a Class

    How do you create a new object named $vehicle from a class named Car in PHP?

    1. $vehicle = new Car();
    2. $vehicle = new object Car();
    3. $vehicle = create Car();
    4. $vehicle = Car-u003Enew();

    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.

  3. Accessing a Public Property

    If a class User has a public property $name, how do you access this property from an object $user?

    1. $user.name
    2. $user['name']
    3. user-u003Ename
    4. $user-u003Ename

    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.

  4. Purpose of Constructors

    What is the purpose of a constructor method like __construct() in a PHP class?

    1. To destroy objects automatically
    2. To initialize object properties
    3. To assign constants
    4. To define static methods

    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.

  5. Implementing an Interface

    In PHP, which keyword is used in a class declaration to indicate it is following an interface’s contract?

    1. implements
    2. extends
    3. includes
    4. inherits

    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.

  6. Private vs Public Property

    Which visibility keyword restricts access to a property so it can only be used within its own PHP class?

    1. public
    2. open
    3. static
    4. private

    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.

  7. Static Methods in PHP

    How do you call a static method sayHello from the class Greetings without instantiating an object?

    1. Greetings.sayHello();
    2. $greet = new Greetings(); $greet-u003EsayHello();
    3. Greetings-u003EsayHello();
    4. Greetings::sayHello();

    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.

  8. Interface Definition

    Which statement best describes a PHP interface?

    1. An interface defines method signatures without implementation.
    2. An interface is used to inherit properties from a parent class.
    3. An interface automatically creates objects.
    4. An interface stores static data only.

    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.

  9. Object Instantiation Output

    Given: class Demo {}; $obj = new Demo(); What type of variable is $obj after this code runs?

    1. An integer
    2. An object of type Demo
    3. A string
    4. An interface

    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.

  10. Accessing Methods from Objects

    How would you call the method getData() of an object $item?

    1. $item-u003Eget_Data();
    2. $item.getData();
    3. $item:getData();
    4. $item-u003EgetData();

    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'.