OOP and objects
Comparing Objects
In PHP, objects can be compared using the == (equality) and === (identity) operators. These operators help determine if objects are equal or identical. Equality (==): two objects are considered equal if they have the same properties and values. Identity (===): Two objects are considered identical if they refer to the same instance of a class.
Inheritance
Inheritance is a mechanism where a new class inherits properties and methods from an existing class. The class that is inherited from is called the parent or base class, and the class that inherits is called the child or derived class.
Interfaces
Interfaces define a contract for classes, specifying methods that must be implemented by any class that implements the interface. Interfaces contain method declarations without implementations.
Object-oriented programming (OOP)
Is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods). In OOP, computer programs are designed by making them out of objects that interact with one another.
Late Static Bindings
Late Static Bindings (LSB) in PHP refer to a feature that allows the use of the static keyword to reference the called class in a context of static inheritance. This feature was introduced in PHP 5.3 and helps in cases where you want to ensure that the called class's context is respected, even when the call is made from a parent class.
Magic Methods
Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo().
Object Cloning
Object cloning in PHP is the process of creating a copy of an existing object. This is achieved using the clone keyword, which duplicates the object's properties and methods.
Objects and references
One of the key-points of PHP OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples. A PHP reference is an alias, which allows two different variables to write to the same value. In PHP, an object variable doesn't contain the object itself as value. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
Overloading
Overloading in PHP provides means to dynamically create properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
Object Iteration
PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. In PHP, this is typically achieved through method overriding and interfaces.
Properties
Properties in PHP are variables that belong to a class. They define the attributes or characteristics of an object created from the class. Properties can have different visibility levels: public, protected, or private, which control how and where they can be accessed.
Serializing objects
serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The methods in an object will not be saved, only the name of the class.
Class
A class is a blueprint for creating objects. It defines properties and methods that the created objects will have.
Abstract Classes
An abstract class in PHP is a class that cannot be instantiated on its own and must be inherited by other classes. Abstract classes are used as a base for other classes to build upon and can define methods that must be implemented by any subclass.
Objects
An object is an instance of a class. It is a self-contained component which consists of methods and properties to make a particular type of data useful.
Anonymous classes
Anonymous classes are useful when simple, one-off objects need to be created.
Constants
Constants are like variables except that once they are defined, they cannot be changed or undefined. Constants are useful for defining values that should not change during the execution of a script.
Constructors
Constructors are special methods in a class that are automatically called when an object is instantiated. They are typically used to initialize properties and perform any setup required for the object.
Covariance and Contravariance
Covariance and contravariance are terms used in programming to describe how the type system handles inheritance and method overriding. They are particularly relevant in object-oriented programming and generic types. PHP introduced support for covariance and contravariance in PHP 7.4. Covariance allows a method to return a more specific type than the one defined in the parent class. Use Case: When overriding a method, the return type can be a subclass of the return type declared in the parent class. Contravariance allows a method to accept parameter types that are more general (less specific) than those defined in the parent class. Use Case: When overriding a method, the parameter type can be a superclass of the parameter type declared in the parent class.
Static Keyword
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. These can also be accessed statically within an instantiated class object. Static properties are variables that belong to the class itself rather than to any object instantiated from the class. They can be accessed without creating an instance of the class. Static methods are functions that belong to the class itself and can be called without creating an instance of the class.
Destructors
Destructors are special methods in a class that are automatically called when an object is destroyed. They are used to perform cleanup tasks, such as closing database connections or freeing resources.
Encapsulation
Encapsulation is the concept of wrapping data (variables) and code (methods) together as a single unit. In OOP, this is achieved through the use of access modifiers (public, private, protected) which control the visibility of properties and methods.
Scope Resolution Operator
The Scope Resolution Operator or in simpler terms, the double colon, is a token that allows access to a constant, static property, or static method of a class or one of its parents. Moreover, static properties or methods can be overriden via late static binding.
Final Keyword
The final keyword prevents child classes from overriding a method or constant by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
Visibility
The visibility of a property, a method or (as of PHP 7.1.0) a constant can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inheriting and parent classes. Members declared as private may only be accessed by the class that defines the member.
Traits
Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.
