Find and fix PHP problems
Guilty Of PHP articles about Find and fix PHP problems
10 articles
-
Measure PHP Attribute Lookup Costs Before Caching Reflection Results
Time the code path with and without an attribute, isolate repeated lookups from object instantiation, and cache retrieved metadata only if the measured workload benefits.
-
Should a Custom PHP Exception Extend Exception or an SPL Subclass?
Choose LogicException for a program-logic failure, RuntimeException for an operational failure, or Exception when that distinction adds little.
-
Throw a PHP Exception When an Operation Fails, Then Catch It at the Caller
Use distinct exceptions for invalid input and an operation that cannot finish, let an intermediate function pass them through, and catch them where a useful response is possible.
-
Specify Required and Optional Array Keys with PHPDoc Shapes
Use `array{id: int, name: string, nickname?: string}` to give PHPStan or Psalm known keys and value types to analyze.
-
Declare a PHP Trait Property Without Conflicting With the Using Class
A trait property becomes part of the using class. In PHP 8.x, a same-named class property needs a compatibility check, not an automatic rejection.
-
Annotate an Array of Task Objects with PHPDoc
Use `Task[]` or `array<Task>` to describe the values PHPStan should expect, and reserve `list<Task>` for arrays whose keys must also start at zero and stay sequential.
-
Restrict Object Classes When Calling PHP unserialize()
Pass an allowed_classes list or false, then check whether the restored value is the expected object or __PHP_Incomplete_Class before using it.
-
Declare a Readonly PHP Class Without Invalid Properties
Use a typed data holder to check declarations, inheritance, traits and writes before converting an existing class.
-
Choose a PHP Static Analyzer and Formatter by the Job You Need Done
Use PHPStan or Psalm to report potential code issues, then choose PHP CS Fixer or PHPCS with PHPCBF for style rules and fixes. Project standards and editor support determine the practical fit.
-
Check a PHP File for Syntax Errors Before You Run It
Submit or open the file in a syntax checker, match its PHP version to your target environment, then use any reported message and line to guide a correction.
