diff --git a/source/_posts/php.md b/source/_posts/php.md index 6a8c890..0d03607 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -913,6 +913,166 @@ 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. +``` + +## PHP Enums (PHP 8.1) + +### Enum Declaration & Use case {.col-span-3} + +```php + "Waiting for approval.", + Status::Approved => "Your request has been approved.", + Status::Rejected => "Your request has been rejected.", + }; +} + +$currentStatus = Status::Approved; +echo getStatusMessage($currentStatus); // Output : Your request has been approved. +``` + +## PHP Date & Time Handling + +### Current date and time +```php +// DateTime in PHP handles both date and time + +format('Y-m-d H:i:s.u'); + +// Output ex: 2024-04-27 14:35:22.123456 +``` + + +### Creating specific date/time objects +```php +format('Y-m-d'); + +// Output : 2024-04-27 + +// Create a time object +$t = new DateTime('15:30:45'); +echo $t->format('H:i:s'); + +// Output : 15:30:45 + +// Create a datetime object +$dt = new DateTime('2024-04-27 15:30:45'); +echo $dt->format('Y-m-d H:i:s'); + +// Output : 2024-04-27 15:30:45 +``` + +### Converting between date formats +```php +format('Y-m-d H:i:s'); + +// Output : 2024-04-27 14:00:0027 + +// Convert a DateTime object to a string +$formatted_str = $dt_obj->format('d/m/Y H:i'); +echo $formatted_str; + +// Output : 27/04/2024 14:00 +``` +### Timestamps and Unix time {.col-span-2} +```php +setTimestamp($timestamp); +echo $dt_from_timestamp->format('Y-m-d H:i:s'); + +// Output ex : 2025-06-18 13:33:03 +``` + + +### Date difference and timedelta {.col-span-1} +```php +diff($date2); +echo $interval->days; + +// Output : 4 + +// Using DateInterval for date arithmetic +$new_date = clone $date1; +$new_date->add(new DateInterval('P10D')); +echo $new_date->format('Y-m-d'); + +// Output : 2024-05-07 +``` + ## Miscellaneous ### Basic error handling @@ -1002,6 +1162,25 @@ See: [Regex in PHP](/regex#regex-in-php) | `a` | Write, append | | `a+` | Read and write, append | +### Super Global Variables {.col-span-2} + +| Variable | Description | +|--------------|:-------------------------------------------------------------| +| `$_SERVER` | Holds information about headers, paths, and script locations | +| `$_GET` | Contains data sent via URL parameters (query string) | +| `$_POST` | Contains data sent via HTTP POST method | +| `$_FILES` | Contains information about uploaded files | +| `$_COOKIE` | Contains cookie data | +| `$_SESSION` | Stores session variables | +| `$_REQUEST` | Contains data from `$_GET`, `$_POST`, and `$_COOKIE` | +| `$_ENV` | Contains environment variables | +| `$GLOBALS` | References all global variables available in the script | + +{.left-text} + +Super Global Variables are built-in variables always available in all scopes. + + ### Runtime defined Constants ```php