A clean and categorized reference of the most useful constants in PHP.
Includes descriptions and sample outputs for context. Fully valid for PHP 8.3 and safe for PHP 8.4.
| Constant | Description | Example Output |
A clean and categorized reference of the most useful constants in PHP.
Includes descriptions and sample outputs for context. Fully valid for PHP 8.3 and safe for PHP 8.4.
| Constant | Description | Example Output |
| <?php | |
| /** | |
| * TimeoutGuard — A lightweight timeout wrapper using PHP ticks. | |
| * | |
| * This lets you interrupt execution of any callable if it exceeds a time limit. | |
| * Works even on Windows (unlike pcntl). | |
| * | |
| * ⚠️ Requires `declare(ticks=1);` | |
| */ |
Instead of defining scopes inside models, create scope classes that can be used in both Eloquent & Scout queries!
| <?php | |
| Artisan::command('generate-tests', function () { | |
| // Define the base path where class files are located | |
| $files = File::allFiles(base_path('app/Classes')); | |
| foreach ($files as $file) { | |
| // Get the relative path of the current class file and apply string manipulations | |
| $path = str($file->getRelativePathname()) | |
| ->replace('.php', '') // Remove the .php extension | |
| ->replace('/', '\\') // Replace directory slashes with namespace slashes |
| <?php | |
| // In Laravel, we have the $with property to automatically load a relationship every time. | |
| // However, selecting a specific column by default for every model query doesn't seem possible... | |
| // but here's a hack I found! | |
| class Post extends Model | |
| { | |
| // To automatically load the 'author' relationship on every query | |
| protected $with = ['author']; |
| <?php | |
| use App\Enums\PostStatus; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Database\Eloquent\Attributes\Attribute; | |
| class Post extends Model | |
| { | |
| // We can easily cast the 'status' column to an enum instance |