PHP Professional Standards & Competencies 4 — Questions and Answers
Question 1: Which Composer command should you run before deploying a PHP application to production?
- composer update
- composer install --no-dev --optimize-autoloader (Correct answer)
- composer dump-autoload
- composer require --dev
Correct answer: composer install --no-dev --optimize-autoloader
`composer install --no-dev --optimize-autoloader` installs only production dependencies and generates an optimized classmap for faster autoloading.
Question 2: What is the purpose of the `composer.lock` file in a professional PHP project?
- Prevent any new packages from being installed
- Pin the exact resolved versions of all dependencies so every install is reproducible (Correct answer)
- Store encrypted credentials for private repositories
- Define the PHP version constraint for the project
Correct answer: Pin the exact resolved versions of all dependencies so every install is reproducible
The lockfile records exact package versions and hashes, ensuring every developer and deployment environment uses the identical dependency tree.
Question 3: In PHP, what is the professional use of interfaces over concrete class type hints in method signatures?
- Interfaces make the code run faster
- Programming to interfaces decouples code from specific implementations, making it easier to swap or mock dependencies (Correct answer)
- Interfaces automatically generate boilerplate code
- PHP requires interfaces for all public methods
Correct answer: Programming to interfaces decouples code from specific implementations, making it easier to swap or mock dependencies
Type-hinting against an interface rather than a concrete class follows the Dependency Inversion Principle and enables substituting alternative implementations without changing the consuming code.
Question 4: Which of the following best describes the 'Boy Scout Rule' applied to PHP development?
- Always write tests before features
- Leave the code cleaner than you found it — fix small issues whenever you touch a file (Correct answer)
- Refactor the entire codebase in one sprint
- Never modify legacy code
Correct answer: Leave the code cleaner than you found it — fix small issues whenever you touch a file
The Boy Scout Rule encourages incremental improvement: rename a confusing variable, remove dead code, or add a missing type hint each time you edit a file.
Question 5: What is a 'feature flag' and why might a professional PHP team use one?
- A PHP flag that disables error reporting in production
- A runtime switch that enables or disables a feature without deploying new code, allowing safe gradual rollout (Correct answer)
- A Composer flag that installs optional packages
- A git branch naming convention for new features
Correct answer: A runtime switch that enables or disables a feature without deploying new code, allowing safe gradual rollout
Feature flags let teams deploy code to production while keeping features hidden, enabling A/B testing, canary releases, and instant rollback without redeployment.
Question 6: Why should a professional PHP developer avoid using `global` variables?
- PHP does not support global variables
- Global variables create hidden dependencies, make code hard to test, and can cause unpredictable state across the application (Correct answer)
- Global variables cause memory leaks in every PHP version
- They are only unavailable in CLI scripts
Correct answer: Global variables create hidden dependencies, make code hard to test, and can cause unpredictable state across the application
Global state makes function behavior depend on external context, breaking encapsulation and making unit testing nearly impossible.
Question 7: What is the professional standard for documenting a public PHP method's parameters and return type?
- Write a multi-paragraph essay in the docblock
- Use PHP 8 native type declarations and add a concise docblock only when the type alone is insufficient (Correct answer)
- Use only inline comments inside the method body
- Documentation is optional for public methods
Correct answer: Use PHP 8 native type declarations and add a concise docblock only when the type alone is insufficient
PHP 8 typed properties and union types often make docblocks redundant; add `@param`/`@return` only to convey information the type system cannot express alone.
Which Composer command should you run before deploying a PHP application to production?