Loading skill
Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Common PHP mistakes and anti-patterns to avoid for cleaner, more maintainable code.
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -3% | 0% |
| case-12 | ✗→✓ | ▲ Improved | 38% | 0% |
| case-22 | ✓→✓ | = Same ✓ | 48% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 3% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 48% | 0% |
php<?php // ❌ Bad: Class does everything class User { public function save() {} public function sendEmail() {} public function generatePDF() {} public function processPayment() {} public function logActivity() {} } // ✅ Good: Single Responsibility class User { // Only user data and basic operations } class UserRepository { public function save(User $user) {} } class EmailService { public function sendWelcomeEmail(User $user) {} }
php<?php // ❌ Bad: Global variables global $db; $users = $db->query('SELECT * FROM users'); // ✅ Good: Dependency injection class UserController { public function __construct( private readonly Database $db ) {} public function index() { $users = $this->db->query('SELECT * FROM users'); } }
php<?php // ❌ Bad: Swallowing exceptions try { $user = $this->getUser($id); } catch (\Exception $e) { // Do nothing } // ✅ Good: Handle or propagate try { $user = $this->getUser($id); } catch (NotFoundException $e) { return $this->respond(['error' => 'User not found'], 404); } catch (\Exception $e) { log_message('error', $e->getMessage()); throw $e; }
php<?php // ❌ Bad if ($user->status === 1) { // What does 1 mean? } // ✅ Good: Use enums or constants enum UserStatus: int { case ACTIVE = 1; case INACTIVE = 2; } if ($user->status === UserStatus::ACTIVE) { // Clear intent }
php<?php // ❌ Bad: Unclear structure $user = [ 'id' => 1, 'name' => 'John', 'data' => ['email' => 'john@example.com'] ]; // ✅ Good: Use DTOs or entities class User { public function __construct( public readonly int $id, public readonly string $name, public readonly string $email ) {} }
php<?php // ❌ Bad: Relying on loose comparison if ($value == true) {} // '1', 'yes', [1] all match if ($count == 0) {} // '', null, false, [] all match // ✅ Good: Strict comparison if ($value === true) {} if ($count === 0) {}
php<?php // ❌ Bad: Returning error codes function getUser(int $id) { $user = $this->db->find($id); return $user ?: false; } // ✅ Good: Throw exceptions function getUser(int $id): User { $user = $this->db->find($id); if (!$user) { throw new NotFoundException('User not found'); } return $user; }
Other measured skills in the registry, with their headline benchmark lift.