You are previewing Learning PHP 7.
O'Reilly logo
Learning PHP 7

Book Description

Learn the art of PHP programming through this example-rich book filled to the brim with tutorials every PHP developer needs to know

About This Book

  • Set up the PHP environment and get started with web programming

  • Leverage the potential of PHP for server-side programming, memory management, and object-oriented programming (OOP)

  • This book is packed with real-life examples to help you implement the concepts as you learn

  • Who This Book Is For

    If you are a web developer or programmer who wants to create real-life web applications using PHP 7, or a beginner who wants to get started with PHP 7 programming, this book is for you. Prior knowledge of PHP, PHP 7, or programming is not mandatory.

    What You Will Learn

  • Set up a server on your machine with PHP

  • Use PHP syntax with the built-in server to create apps

  • Apply the OOP paradigm to PHP to write richer code

  • Use MySQL to manage data in your web applications

  • Create a web application from scratch using MVC

  • Add tests to your web application and write testable code

  • Use an existing PHP framework to build and manage your applications

  • Build REST APIs for your PHP applications

  • Test the behavior of web applications with Behat

  • In Detail

    PHP is a great language for building web applications. It is essentially a server-side scripting language that is also used for general purpose programming. PHP 7 is the latest version with a host of new features, and it provides major backwards-compatibility breaks.

    This book begins with the fundamentals of PHP programming by covering the basic concepts such as variables, functions, class, and objects. You will set up PHP server on your machine and learn to read and write procedural PHP code. After getting an understanding of OOP as a paradigm, you will execute MySQL queries on your database. Moving on, you will find out how to use MVC to create applications from scratch and add tests. Then, you will build REST APIs and perform behavioral tests on your applications.

    By the end of the book, you will have the skills required to read and write files, debug, test, and work with MySQL.

    Style and approach

    This book begins with the basics that all PHP developers use every day and then dives deep into detailed concepts and tricks to help you speed through development. You will be able to learn the concepts by performing practical tasks and implementing them in your daily activities, all at your own pace.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Learning PHP 7
      1. Table of Contents
      2. Learning PHP 7
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Setting Up the Environment
        1. Setting up the environment with Vagrant
          1. Introducing Vagrant
          2. Installing Vagrant
          3. Using Vagrant
        2. Setting up the environment on OS X
          1. Installing PHP
          2. Installing MySQL
          3. Installing Nginx
          4. Installing Composer
        3. Setting up the environment on Windows
          1. Installing PHP
          2. Installing MySQL
          3. Installing Nginx
          4. Installing Composer
        4. Setting up the environment on Ubuntu
          1. Installing PHP
          2. Installing MySQL
          3. Installing Nginx
        5. Summary
      9. 2. Web Applications with PHP
        1. The HTTP protocol
          1. A simple example
          2. Parts of the message
            1. URL
            2. The HTTP method
            3. Body
            4. Headers
            5. The status code
          3. A more complex example
        2. Web applications
          1. HTML, CSS, and JavaScript
        3. Web servers
          1. How they work
          2. The PHP built-in server
          3. Putting things together
        4. Summary
      10. 3. Understanding PHP Basics
        1. PHP files
        2. Variables
          1. Data types
        3. Operators
          1. Arithmetic operators
          2. Assignment operators
          3. Comparison operators
          4. Logical operators
          5. Incrementing and decrementing operators
          6. Operator precedence
        4. Working with strings
        5. Arrays
          1. Initializing arrays
          2. Populating arrays
          3. Accessing arrays
          4. The empty and isset functions
          5. Searching for elements in an array
          6. Ordering arrays
          7. Other array functions
        6. PHP in web applications
          1. Getting information from the user
          2. HTML forms
          3. Persisting data with cookies
          4. Other superglobals
        7. Control structures
          1. Conditionals
          2. Switch…case
          3. Loops
            1. While
            2. Do…while
            3. For
            4. Foreach
        8. Functions
          1. Function declaration
          2. Function arguments
          3. The return statement
          4. Type hinting and return types
        9. The filesystem
          1. Reading files
          2. Writing files
          3. Other filesystem functions
        10. Summary
      11. 4. Creating Clean Code with OOP
        1. Classes and objects
          1. Class properties
          2. Class methods
          3. Class constructors
          4. Magic methods
        2. Properties and methods visibility
          1. Encapsulation
        3. Static properties and methods
        4. Namespaces
        5. Autoloading classes
          1. Using the __autoload function
          2. Using the spl_autoload_register function
        6. Inheritance
          1. Introducing inheritance
          2. Overriding methods
          3. Abstract classes
        7. Interfaces
          1. Polymorphism
        8. Traits
        9. Handling exceptions
          1. The try…catch block
          2. The finally block
          3. Catching different types of exceptions
        10. Design patterns
          1. Factory
          2. Singleton
        11. Anonymous functions
        12. Summary
      12. 5. Using Databases
        1. Introducing databases
          1. MySQL
        2. Schemas and tables
          1. Understanding schemas
          2. Database data types
            1. Numeric data types
            2. String data types
            3. List of values
            4. Date and time data types
          3. Managing tables
        3. Keys and constraints
          1. Primary keys
          2. Foreign keys
          3. Unique keys
          4. Indexes
        4. Inserting data
        5. Querying data
        6. Using PDO
          1. Connecting to the database
          2. Performing queries
          3. Prepared statements
        7. Joining tables
        8. Grouping queries
        9. Updating and deleting data
          1. Updating data
          2. Foreign key behaviors
          3. Deleting data
        10. Working with transactions
        11. Summary
      13. 6. Adapting to MVC
        1. The MVC pattern
        2. Using Composer
          1. Managing dependencies
          2. Autoloader with PSR-4
          3. Adding metadata
          4. The index.php file
        3. Working with requests
          1. The request object
          2. Filtering parameters from requests
          3. Mapping routes to controllers
          4. The router
            1. URLs matching with regular expressions
            2. Extracting the arguments of the URL
            3. Executing the controller
        4. M for model
          1. The customer model
          2. The book model
          3. The sales model
        5. V for view
          1. Introduction to Twig
          2. The book view
          3. Layouts and blocks
          4. Paginated book list
          5. The sales view
          6. The error template
          7. The login template
        6. C for controller
          1. The error controller
          2. The login controller
          3. The book controller
          4. Borrowing books
          5. The sales controller
        7. Dependency injection
          1. Why is dependency injection necessary?
          2. Implementing our own dependency injector
        8. Summary
      14. 7. Testing Web Applications
        1. The necessity for tests
          1. Types of tests
          2. Unit tests and code coverage
        2. Integrating PHPUnit
          1. The phpunit.xml file
          2. Your first test
          3. Running tests
        3. Writing unit tests
          1. The start and end of a test
          2. Assertions
          3. Expecting exceptions
          4. Data providers
        4. Testing with doubles
          1. Injecting models with DI
          2. Customizing TestCase
          3. Using mocks
        5. Database testing
        6. Test-driven development
          1. Theory versus practice
        7. Summary
      15. 8. Using Existing PHP Frameworks
        1. Reviewing frameworks
          1. The purpose of frameworks
          2. The main parts of a framework
        2. Other features of frameworks
          1. Authentication and roles
          2. ORM
          3. Cache
          4. Internationalization
        3. Types of frameworks
          1. Complete and robust frameworks
          2. Lightweight and flexible frameworks
        4. An overview of famous frameworks
          1. Symfony 2
          2. Zend Framework 2
          3. Other frameworks
        5. The Laravel framework
          1. Installation
          2. Project setup
          3. Adding the first endpoint
          4. Managing users
            1. User registration
            2. User login
            3. Protected routes
          5. Setting up relationships in models
          6. Creating complex controllers
          7. Adding tests
        6. The Silex microframework
          1. Installation
          2. Project setup
            1. Managing configuration
            2. Setting the template engine
            3. Adding a logger
          3. Adding the first endpoint
          4. Accessing the database
        7. Silex versus Laravel
        8. Summary
      16. 9. Building REST APIs
        1. Introducing APIs
        2. Introducing REST APIs
        3. The foundations of REST APIs
          1. HTTP request methods
            1. GET
            2. POST and PUT
            3. DELETE
          2. Status codes in responses
            1. 2xx – success
            2. 3xx – redirection
            3. 4xx – client error
            4. 5xx – server error
          3. REST API security
            1. Basic access authentication
            2. OAuth 2.0
        4. Using third-party APIs
          1. Getting the application's credentials
          2. Setting up the application
          3. Requesting an access token
          4. Fetching tweets
        5. The toolkit of the REST API developer
          1. Testing APIs with browsers
          2. Testing APIs using the command line
        6. Best practices with REST APIs
          1. Consistency in your endpoints
          2. Document as much as you can
          3. Filters and pagination
          4. API versioning
          5. Using HTTP cache
        7. Creating a REST API with Laravel
          1. Setting OAuth2 authentication
            1. Installing OAuth2Server
            2. Setting up the database
            3. Enabling client-credentials authentication
            4. Requesting an access token
          2. Preparing the database
          3. Setting up the models
          4. Designing endpoints
          5. Adding the controllers
        8. Testing your REST APIs
        9. Summary
      17. 10. Behavioral Testing
        1. Behavior-driven development
          1. Introducing continuous integration
          2. Unit tests versus acceptance tests
          3. TDD versus BDD
          4. Business writing tests
        2. BDD with Behat
          1. Introducing the Gherkin language
          2. Defining scenarios
            1. Writing Given-When-Then test cases
            2. Reusing parts of scenarios
          3. Writing step definitions
            1. The parameterization of steps
          4. Running feature tests
        3. Testing with a browser using Mink
          1. Types of web drivers
          2. Installing Mink with Goutte
          3. Interaction with the browser
        4. Summary
      18. Index