Learning PHP 7
eBook - ePub

Learning PHP 7

Antonio Lopez

Share book
  1. 414 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning PHP 7

Antonio Lopez

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learning PHP 7 an online PDF/ePUB?
Yes, you can access Learning PHP 7 by Antonio Lopez in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in PHP. We have over one million books available in our catalogue for you to explore.

Information

Year
2016
ISBN
9781785880544
Edition
1

Learning PHP 7


Table of Contents

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

Learning PHP 7

Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be ...

Table of contents