---
title: Object-Oriented Principles in PHP
kind: note
description: 'Link 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…'
words: 576
readingMinutes: 3
created: '2023-04-08T00:00:00.000Z'
updated: '2024-06-12T18:20:49+02:00'
website: https://laracasts.com/series/object-oriented-principles-in-php
---
## Link

<https://laracasts.com/series/object-oriented-principles-in-php>

# Course notes: <span class="dead-link">Laracasts</span> > [Learn Laravel Path](https://laracasts.com/path) > [Object-Oriented Principles in PHP](https://laracasts.com/series/object-oriented-principles-in-php)
## [01 - Classes](https://laracasts.com/series/object-oriented-principles-in-php/episodes/1)
- Think of a class like a blueprint with functionality common to all instances/objects.
- Nouns make most sense as class names.
## [02 - Objects](https://laracasts.com/series/object-oriented-principles-in-php/episodes/2)
- The constructor function is called `__construct`.
- A public static method is also called a "class method".
- The [`...` operator](https://www.php.net/manual/en/migration56.new-features.php) (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](/notes/php) 5.6.
## [03 - Inheritance](https://laracasts.com/series/object-oriented-principles-in-php/episodes/3)
- 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](https://wiki.php.net/rfc/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](/notes/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](https://www.php.net/manual/en/language.oop5.visibility.php).
## [04 - Abstract Classes](https://laracasts.com/series/object-oriented-principles-in-php/episodes/4)
- `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](https://laracasts.com/series/object-oriented-principles-in-php/episodes/5)
- 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](https://laracasts.com/series/object-oriented-principles-in-php/episodes/6)
- 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](https://laracasts.com/series/object-oriented-principles-in-php/episodes/7)
- Object composition = a class has a reference to an object of another class
## [08 - Value Objects and Mutability](https://laracasts.com/series/object-oriented-principles-in-php/episodes/8)
- 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](https://laracasts.com/series/object-oriented-principles-in-php/episodes/9)
- 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.