{ claus.conrad }

Object-Oriented Principles in PHP


Course notes: Laracasts > Learn Laravel Path > Object-Oriented Principles in PHP

01 - Classes

  • Think of a class like a blueprint with functionality common to all instances/objects.
  • Nouns make most sense as class names.

02 - Objects

  • The constructor function is called __construct.
  • A public static method is also called a “class method”.
  • The ... operator (a.k.a. “splat” in other languages) allows accepting a variable number of arguments, which are accessible as an array. It was introduced in PHP 5.6.

03 - Inheritance

  • The extends keyword defines a child class, expressing a “is-a” relationship with the parent class.
  • Creating a child class is called “subclassing”.
  • The use keyword, in addition to being related to namespaces, also has another meaning in closures, making variables from the parent scope visible inside a lambda function.
  • array_map runs a callback against each item of an array, returning a new array.
  • array_column selects a property of each item of an array, returning a new array.
  • array_sum sums all items in a (numeric) array.
  • class_basename() is a Laravel helper function that returns the name of the actual class (i.e. if a subclass is instantiated, code in the parent class can use this method to know the name of the child class that the object was instantiated from).
  • Child classes can override parent members which have public or protected visibility.

04 - Abstract Classes

  • str_replace finds and replaces strings within strings.
  • preg_replace uses regular expressions to find and replace patterns in strings.
  • strtolower lowercases a string.
  • The abstract keyword:
    • makes a class “un-instantiable” (but it can be subclassed)
    • declares that a method must be implemented in a child class

05 - Handshakes and Interfaces

  • Being a dynamic language, PHP supports “duck typing”: If something walks like a duck and quacks like a duck, it probably is a duck. The disadvantage is that IDEs cannot provide type checking and the code becomes less robust.
  • The interface keyword defines an interface.
  • Think of an interface as a class without behavior - only method signatures.
  • The implements keyword declares that a class implements an interface.
  • Think of “duck typing” as a handshake and of interfaces like formal contracts.

06 - Encapsulation

  • Both methods and properties can have a visibility (public/protected/private) assigned.
  • The default visibility is public (e.g. var and public have the same meaning).
  • Sometimes you have to make something public because it needs to be accessible by another part of your application, even though you don’t want to expose the functionality to third-party consumers of your API. In such cases developers often tag (annotate) the methods intended for third parties with @api or similar.

07 - Object Composition and Abstractions

  • Object composition = a class has a reference to an object of another class

08 - Value Objects and Mutability

  • A value object protects simpler types (e.g., integers, strings, etc.) from being set to values that are invalid in the specific context.
  • To modify the values in a value object, create a new instance, add methods to make the values mutable, or add methods that return a new instance with the desired values.
  • Another use case for value objects is to group properties that belong together, such as the x and y of a set of coordinates, or the currency and the numeric value for an amount of money.

09 - Exceptions

  • If you might reasonably expect some behavior, it shouldn’t throw an exception (but e.g. return false instead).
  • Always protect the state/data at the lowest level.
  • Create custom Exception subclasses only for important cases where a clear business rule is being broken.