PHP: The Right Way

repository·gh-pages·Indexed 27 days ago

https://github.com/codeguy/php-the-right-way

A 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.

Tokens
27.8K
Snippets
69
Records
168
Agent score
94%

What's inside php-the-right-way

  1. Understand Inversion of Control (IoC)

    gh-pages
    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.
  2. Explore complementary PHP testing tools

    gh-pages

    Beyond 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.
  3. Use component repositories for shared code

    gh-pages

    To 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).

  4. Choose between APCu and Memcached for object caching

    gh-pages

    When 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.
  5. Understand the Standard PHP Library (SPL)

    gh-pages

    The 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.
  6. Benefits of using templates in PHP applications

    gh-pages

    Using templates provides three primary advantages for application architecture:

    1. 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.
    2. Improved Organization and Reuse: Templates organize presentation code into dedicated files, typically stored in a views folder. This structure supports code reuse through partials—smaller, reusable template files (like a site header or footer) that are included within larger page templates.
    3. 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.
  7. Choose a PHP framework type

    gh-pages

    When 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.
  8. Explore PHP PaaS Providers

    gh-pages

    If 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