PHP: The Right Way
repository·gh-pages·Indexed 27 days ago
https://github.com/codeguy/php-the-right-wayA community-driven project providing high-quality, credible information and best practices for PHP developers. It covers essential topics including installation on macOS, Windows, and Linux, project directory organization, coding standards (PSR-1, PSR-12), and tools for code auditing and fixing such as PHP_CodeSniffer, phpcbf, and PHP Coding Standards Fixer.
What's inside php-the-right-way
- Inversion of Control is a design principle used to loosen dependencies by separating organizational control from the objects themselves. Instead of an object being responsible for instantiating its own dependencies, those dependencies are controlled and instantiated elsewhere in the system (typically via Dependency Injection). This prevents the common pitfall of simply moving dependencies into a base controller or super object, and instead allows for injecting only the specific dependencies required at the moment they are needed.
Explore complementary PHP testing tools
gh-pagesBeyond standard testing and behavior-driven frameworks, several specialized libraries can enhance your testing suite:
- Browser Automation: Use [Selenium] for browser automation, which can be [integrated with PHPUnit].
- Mocking Frameworks:
- [Mockery]: A Mock Object Framework compatible with [PHPUnit] or [PHPSpec].
- [Prophecy]: A powerful, opinionated PHP object mocking framework integrated with [PHPSpec] and usable with [PHPUnit].
- [php-mock]: A library specifically designed to help mock native PHP functions.
- Test Quality & Compatibility:
- [Infection]: A PHP implementation of [Mutation Testing] used to measure the effectiveness of your existing tests.
- [PHPUnit Polyfills]: A library for creating PHPUnit tests that are cross-version compatible when running against a range of PHPUnit versions.
Understand the role of Templating in MVC
gh-pagesIn the Model-View-Controller (MVC) architecture, templates (often referred to as "views") are used to separate controller and domain logic from presentation logic. While templates typically contain the HTML of an application, they can also be used to generate other formats like XML.Use component repositories for shared code
gh-pagesTo create, distribute, and implement shared PHP code, use established component repositories. The two primary repositories are:
- Packagist: The standard repository for PHP packages.
- PEAR: An older, established repository for PHP extensions and packages.
Both repositories provide command-line tools to manage installation and upgrade processes (refer to the Dependency Management section for details on using these tools).
Understand Platform as a Service (PaaS) for PHP deployment
gh-pagesPlatform as a Service (PaaS) provides the system and network architecture required to run PHP applications on the web. Using a PaaS allows for deploying, hosting, and scaling PHP applications with little to no manual configuration for the underlying PHP environment or frameworks.Use Xdebug for Code Coverage and Profiling
gh-pagesXdebug is not only for interactive debugging; it can also be used to perform code coverage analysis and code profiling. This allows tools like PHPUnit to measure test coverage or KCacheGrind to analyze performance profiles.Choose between APCu and Memcached for object caching
gh-pagesWhen deciding on an object caching strategy, consider your architecture:
- APCu: Best for single-server applications where low latency is the priority. It is easy to set up but data is local to the server.
- Memcached: Best for distributed systems or multi-server environments. It runs as a separate service and can be accessed across a network, allowing multiple systems to share a central, high-speed data store.
- Redis: An alternative networked memory object caching system.
Understand the Standard PHP Library (SPL)
gh-pagesThe Standard PHP Library (SPL) is a built-in collection of classes and interfaces packaged with PHP. It is primarily used for managing common data structures and implementing efficient traversal patterns.
Key components include:
- Data Structure Classes: Pre-built implementations for structures like stacks, queues, and heaps.
- Iterators: Objects that allow you to traverse data structures or your own custom classes, provided they implement the appropriate SPL interfaces.
Benefits of using templates in PHP applications
gh-pagesUsing templates provides three primary advantages for application architecture:
- Separation of Concerns: Templates separate presentation logic from application logic. Templates should only be responsible for displaying formatted content and should not handle data lookup, persistence, or complex business tasks. This allows server-side developers (working on controllers and models) and designers (working on markup) to work independently.
- Improved Organization and Reuse: Templates organize presentation code into dedicated files, typically stored in a
viewsfolder. This structure supports code reuse through partials—smaller, reusable template files (like a site header or footer) that are included within larger page templates. - Enhanced Security: Many templating libraries provide automatic escaping of user-generated content to prevent security vulnerabilities. Some advanced libraries offer sand-boxing, restricting template designers to a specific whitelist of variables and functions.
Choose a PHP framework type
gh-pagesWhen deciding whether to use a framework or plain PHP, consider the three main categories of PHP frameworks based on your project requirements:
- Micro Frameworks: Lightweight wrappers designed to route HTTP requests to callbacks, controllers, or methods as quickly as possible. They are ideal for building remote HTTP services and may include minimal helper libraries like basic database wrappers.
- Full-Stack Frameworks: Comprehensive frameworks that provide a wide array of features bundled together, such as Object-Relational Mappers (ORMs) and Authentication packages.
- Component Frameworks: Collections of specialized, single-purpose libraries. These can be combined to construct either a micro-framework or a full-stack framework architecture.
Explore PHP PaaS Providers
gh-pagesIf you are looking for Platform-as-a-Service (PaaS) providers to host PHP applications, the following services are recommended:
- Amezmo
- AWS Elastic Beanstalk
- Bref Cloud
- Clever Cloud
- Cloudways
- DigitalOcean App Platform
- Divio
- Engine Yard Cloud
- fortrabbit
- Google App Engine
- Heroku
- IBM Cloud
- Lumen
- Microsoft Azure
- Pivotal Web Services
- Platform.sh
- Red Hat OpenShift
- Virtuozzo
Understand Object-oriented Programming in PHP
gh-pagesPHP provides a comprehensive object-oriented programming (OOP) model. Key features available for use include:
- Classes and Abstract Classes
- Interfaces
- Inheritance
- Constructors
- Cloning
- Exceptions
- Traits (available since PHP 5.4)