diff --git a/source/_posts/php.md b/source/_posts/php.md index 6a8c890..72e31ec 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -913,6 +913,48 @@ class Cls implements Foo, Bar } ``` +## PHP Trait + +### Trait declaration & Use case {.col-span-3} +```php +// Declare Trait allows reuse of code +// across multiple classes for common characteristics or use cases + +format('Y-m-d H:i:s') . " | " . $message; + } +} + +// Use case : +// Call trait with "use in Class" + +class User { + use Logger; + + public function createUser() { + // User creation logic + $this->log("User created."); + } +} + +class Product { + use Logger; + + public function createProduct() { + // Product creation logic + $this->log("Product created."); + } +} + +$user = new User(); +$user->createUser(); // Output ex: | 2025-06-18 14:06:09 | User created. + +$product = new Product(); +$product->createProduct(); // Output ex: | 2025-06-18 14:06:09 | Product created. +``` ## Miscellaneous ### Basic error handling