PHP OOP Terminology
What is a static method?
A static method is one that is called on a class, not on an object. Such methods cannot access properties. The name of a static method is the class name followed by two colons and the function name. Example: HTML::p("Hello, worlds");
How do you set a visibility?
By using access modifiers: public, private, protected
When the child and parent classes have properties or methods with the same name, the child class takes precedence. How would you access the overridden method on the object's parent class?
By using the parent::method() notation. // Example: parent::birthday(); // call parent class's birthday() method
How do you ensure you're calling the method on the current class?
By using the self::method() notation. // Example: self::method(); call this class's birthday() method.
What is an interface?
Interfaces provide a way for defining contracts to which a class adheres. The interface provides method prototypes and constants, and any class that implements the interface must provide implementations for all methods in the interface.
What does the access modifier public do?
It allows properties and methods to be accessible outside the object's scope. Note: if access is not defined, public is the default.
What does the function serialize() do?
It converts an object to bytestream representation.
What does the clone operator do?
It creates a true copy of an object.
What does the class_exists() function do?
It determines whether a class exists. It takes in a string and returns a Boolean value. // Example: $doesClassExist = class_exists(classname); $classes = get_declared_classes(); $doesClassExist = in_array(classname, $classes);
What does the get_parent_class() function do?
It finds a class's parent class. // Example: $superclass = get_parent_class(classname);
What does the get_class() function do?
It gets the class
What does the get_class_methods() function do?
It gets the methods that exist in a class (including those that are inherited from superclasses). This function takes a class name as a parameter and returns an array.
What does the get_class_vars() function do?
It gets the properties that exist in a class (including those that are inherited from superclasses). This function takes a class name as a parameter and returns an array.
What is a destructor?
It is a method that destroys the object. Because PHP automagically cleans up all resources when they fall out of scope and at the end of a script's execution, their application is limited.
What does the __sleep() method do?
It is called on an object just before serialization. It can perform any cleanup necessary to preserve the object's state, such as closing database connection, writing out unsaved persistent data, and so on.
What does the access modifier private do?
It only allows properties and methods on an instance to be accessed within the the same class.
What is a constructor?
It's a special function that initializes he properties of the class. It's a function in the class called __construct().
What is a class?
It's a template for building objects. In the message board example, the class is the structure of the user (first name, last name, password, etc.)
What is an object?
It's an instance (or occurence) of a class. In the example of the message board, it's an actual user data structure with attached code.
What does the __wakeup() method do?
It's called immediately after an object is created from a bytestream. The method can take and action it requires, such as reopening database connections and other initialization tasks.
What is introspection?
It's the ability of a program to examine an object's characteristics, such as its name, parent class (if any), properties, and methods. Examples of introspection: get_declared_class() get_class_methods() get_class_vars()
What does the access modifier protected do?
Methods and properties can only be called from within the object's class methods and the class methods of classes inheriting from the class.
What is serialization?
Serializing an object means converting it to bytestream representation that can be stored in a file. This is useful for persistent data; for example, PHP sessions automatically save and restore objects.
What is an abstract method?
The abstract method is a mechanism for declaring that certain methods on the class must be implemented by subclasses. The implementation of those methods is not defined in the parent class. I a class has any methods in it defined as abstract, you must also declare the class as an abstract class.
What Are Properties in OOP?
The data associated with an object. When you define a class, you define the names of it properties.
What is a method?
The functions associated with an object. When you define a class, you give the code for its methods.
What does the method_exists() function do?
This can be used to ensure that a method exists within an object.
What is encapsulation?
This is the idea that a class provides certain methods (the interface) to the code that uses its objects, so the outside code does not directly access the data structures of those objects.
What is a trait?
Traits provide a mechanism for reusing code outside of a class hierarchy. Traits allow you to share functionality across different classes that don't (and shouldn't) share a common ancestor in a class hierarchy.
How do you inherit the properties and methods from another class?
Use the extends keyword in the class definition, followed by the name of the base class. // Example: class Person { public $name, $address, $age; } class Employee extends Person { public $postion, $slary; }
How do you call a constant from outside the class?
Using :: followed by the name of the constant. // Example: echo PaymentMethod::TYPE_CREDITCARD;
How do you declare a constant in a class?
Using const // Example: class PaymentMethod { const TYPE_CREDITCARD = 0; }
Syntax for abstract methods
abstract class Component { abstract function printOutput(); } class Image Component extends Component { function printOutput() { echo "Pretty picture"; } }
What is the syntax of a constructor?
class Person { function __construct($name, $age) { $this->name = $name; $this->age = $age; } }
What is the syntax for an interface?
interface Printable { function printOutput(); } class ImageComponent implements Printable { function printOutput() { echo "printing an image..."; } }
What is the syntax for a trait?
trait Logger { public function_log(&logString) { $className = __CLASS__; echo date("Y-m-d h:m:s", time()).": [{$className}]{$logString}"; } } class User { use Logger; public $name; function __construct($name=' ') { $this->name=$name; $this->log("Created user '{$this->name}'"); }
How do you declare a constant within a method?
using the self keyword.