POO en PHP (6) - Les méthodes magiques

13 Oct 2019 · 11 min
11:15

The video is a French instructional tutorial on PHP's magic methods, presented by Yamossoun Ayemou Bernard. It begins with a brief reminder that constructors (construct) and destructors (destruct) are magic methods that run automatically. The lecturer then introduces a custom class PHPString with private attributes `str` and `length`, showing how to initialize them in the constructor. Through live code examples, he demonstrates the limitations of accessing private properties from outside the class and then introduces each magic method one by one: - __get – automatically invoked when reading an inaccessible property; the method receives the property name, can perform checks, and returns the appropriate value, allowing controlled read access while preserving encapsulation. - __set – automatically invoked when writing to an inaccessible property; receives the property name and the new value, enabling controlled mutation of private attributes. - __toString – invoked when an object is treated as a string (e.g., echo); returns a string representation, here the original constructor value. - __isset – mirrors the built‑in `isset()` function; called when checking existence of a property, allowing the class to report true/false based on internal state. - __unset – mirrors the built‑in `unset()` function; called when attempting to delete a property, enabling the class to remove or nullify the internal attribute. For each method, the speaker writes the method signature, explains the double‑underscore naming convention, runs the code, and shows the resulting output, emphasizing that magic methods must be used carefully to avoid breaking encapsulation. He concludes by encouraging viewers to consult the official PHP documentation (php.net) for the full list of magic methods. The tutorial’s purpose is to teach PHP developers how to leverage magic methods to manage private data, customize object behavior, and maintain encapsulation while providing convenient syntactic shortcuts.

Chapters

More from Yamossoun

8:56
youtube.com

Compteur synchrone - Logique séquentielle

2w ago
11:58
youtube.com

Compteur asynchrone et diviseur de fréquence - Logique séquentielle

3w ago
11:37
youtube.com

Les registres (parallèle et à décalage) - Logique séquentielle

1mo ago
6:14
youtube.com

Bascule T (Toggle) - Logique séquentielle

1mo ago
12:05
youtube.com

Bascule JK - Logique séquentielle

1mo ago
9:17
youtube.com

Bascule D flip-flop active sur front montant - Logique séquentielle

1mo ago
10:08
youtube.com

Bascule D Latch - Logique séquentielle

1mo ago
8:13
youtube.com

Bascule RS - Logique séquentielle

1mo ago
5:04
youtube.com

Logique séquentielle - Introduction

2mo ago
1:15
youtube.com

Le codeur - Logique combinatoire #codeur #logique #circuit #booleanlogic

2mo ago
1:56
youtube.com

Algorithme de calcule de la racine carrée par dichotomie #dichotomy #squareroot #algorithm

3mo ago
See all content