Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP THE WRONG WAY

PHP THE WRONG WAY

This slide will talk about the right way in PHP Programming

There’s a lot of outdated information on the Web that leads new PHP users astray, propagating bad practices and insecure code. PHP: The Right Way is an easy-to-read, quick reference for PHP popular coding standards, links to authoritative tutorials around the Web and what the contributors consider to be best practices at the present time.

There is no canonical way to use PHP. This website aims to introduce new PHP developers to some topics which they may not discover until it is too late, and aims to give seasoned pros some fresh ideas on those topics they’ve been doing for years without ever reconsidering. This website will also not tell you which tools to use, but instead offer suggestions for multiple options, when possible explaining the differences in approach and use-case.

https://phptherightway.com

Avatar for Dolly Aswin

Dolly Aswin

May 22, 2021

More Decks by Dolly Aswin

Other Decks in Programming

Transcript

  1. Getting Started Code Style Guide Language Highlights Dependency Management Coding

    Practices Dependency Injection Databases Templating Errors and Exceptions Security Testing Servers and Deployment Virtualization Caching Documenting Your Code
  2. GETTING STARTED Start with current stable release Where do I

    put my stuff DocumentRoot should point to public Third party libraries, as installed by composer, belong in the vendor Unit tests should be in the tests directory
  3. CODE STYLE Coding Standard PSR-1 (PHP-FIG) PSR-12 (PHP-FIG) Zend Symfony

    PEAR Tools PHP Coding Standard Fixer (phpcs/php-cs-fixer) PHP Code Beautifier and Fixer (phpcbf)
  4. LANGUAGE HIGHLIGHT Progamming Paradigms Object Oriented Programming Functional Programming Meta

    Programming Namespaces PSR-0 & PSR-4 Standard PHP Library (SPL) Packaged with PHP and provides a collection of Classess and Interfaces. It is made up primarily of commonly needed data structure classes (stack, queue, heap, and so on), and Iterators. Command Line Interface PHP also useful for scripting command line interface (CLI) programs Xdebug Allows you to trace the execution of your code and monitor the contents of the stack
  5. DEPENDENCY MANAGEMENT Composer and Packagist Composer is analogous to NPM

    in the node.js world, or Bundler in the Ruby world. PEAR A veteran package manager that some PHP developers enjoy is PEAR composer.json, composer install, composer.lock and composer update
  6. CODING PRACTICES Basics Comparison Operators (==) (===) Conditional Statements (if,

    switch) Global Namespaces Refer to the global function by using a backslash before the function name (\fopen()) Strings Concatenation String Types (Single Quoted, Double Quoted, Heredoc, Nowdoc) Date and Time (DateTime, DateInterval) Working With UTF-8 UTF-8 at PHP Level (mbstring module) UTF-8 at Database Level UTF-8 at Browser Level Design Pattern
  7. DEPENDENCY INJECTION Basic Concept <?php namespace Database ; class Database

    { protected $adapter ; public function __construct( ) { $this->adapter = new MySqlAdapter ; } } class MysqlAdapter { } <?php namespace Database ; class Database { protected $adapter ; public function __construct(MySqlAdapter $adapter ) { $this->adapter = $adapter ; } } class MysqlAdapter { }
  8. DATABASE PDO Extension PDO is a database connection abstraction library

    — built into PHP since 5.1.0 — that provides a common interface to talk with many different databases. Abstraction Layer Atlas Aura SQL Doctrine 2 DBAL Propel Zend DB
  9. TEMPLATING Libraries Aura.View (native) Blade (compiled, framework specific) Brainy (compiled)

    Dwoo (compiled) Latte (compiled) Mustache (compiled) PHPTAL (compiled) Plates (native) Smarty (compiled) Twig (compiled) Zend View (native, framework specific)
  10. ERRORS AND EXCEPTIONS Error Severity E_ERROR E_NOTICE E_WARNING Changing PHP’s

    Error Reporting Behavior error_reporting(E_ERROR | E_WARNING); Inline Error Suppression echo @$foo[‘bar’]; ErrorException throw new \ErrorException()
  11. ERRORS AND EXCEPTIONS SPL Exceptions The generic Exception class provides

    very little debugging context for the developer; however, to remedy this, it is possible to create a specialized Exception type by sub-classing the generic Exception class, eg: BadFunctionCallException, BadMethodCallException, InvalidArgumentException, … https://www.php.net/manual/en/spl.exceptions.php
  12. SECURITY Web Application Security PHP Versions Input Filtering & Output

    Escaping Missing Authentication & Authorization Input Validation Configuration Files Don’t commit sensitive information (credentials, api token) to repository It is recommended that you store your configuration information where it cannot be accessed directly and pulled in via the file system. Error Reporting Development display_erros = O n display_startup_erros = O n error_reporting = 1 log_errors = On Production display_erros = Of f display_startup_erros = Of f error_reporting = E_AL L log_errors = On
  13. TESTING Unit Testing PHPUnit is the de-facto testing framework for

    writing unit tests for PHP applications Functional Testing Selenium Codeception
  14. SERVERS AND DEPLOYMENT Platform as a Service PaaS provides the

    system and network architecture necessary to run PHP applications on the web. This means little to no configuration for launching PHP applications or PHP frameworks. Virtual or Dedicated Servers Heroku Platform.sh ServerPilot If you are comfortable with systems administration, or are interested in learning it, virtual or dedicated servers give you complete control of your application’s production environment. Shared Servers Make sure your shared servers are offering the latest versions of PHP, check out PHP Versions.
  15. SERVERS AND DEPLOYMENT Building and Deploying Your Application Dependency Management

    Compilation, magnification assets Running Tests Creating Documentation Packaging Deployment Deployment Tools Phing Capistrano Ansistrano Deployer Rocketer