Merge pull request #683 from LGD-P/php_adds

Php adds
This commit is contained in:
Fechin authored and GitHub committed 2025-06-19 08:39:47 +08:00
commit 5ab56e3554
1 file changed
+179
+179
View File
@@ -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
<?php
trait Logger {
public function log($message) {
$date = new DateTime();
echo "| ". $date->format('Y-m-d H:i:s') . " | " . $message;
}
}
// Use case :
// Call trait with "use <trait name> 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
<?php
// enum is a data type that allows you to define a set of named constants,
// representing a fixed group of related values
enum Status {
case Pending;
case Approved;
case Rejected;
}
// Use case
function getStatusMessage(Status $status): string {
return match($status) {
Status::Pending => "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
<?php
$now = new DateTime();
echo $now->format('Y-m-d H:i:s.u');
// Output ex: 2024-04-27 14:35:22.123456
```
### Creating specific date/time objects
```php
<?php
// Create a date object
$d = new DateTime('2024-04-27');
echo $d->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
<?php
// Convert a string to a DateTime object
$date_str = "2024-04-27 14:00";
$dt_obj = new DateTime($date_str);
echo $dt_obj->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
<?php
// Get current timestamp
$timestamp = time();
echo $timestamp;
// Output ex: 1750253583
// Convert timestamp back to DateTime
$dt_from_timestamp = (new DateTime())->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
<?php
$date1 = new DateTime('2024-04-27');
$date2 = new DateTime('2024-05-01');
$interval = $date1->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