50% found this document useful (2 votes)
513 views18 pages

Mastering PHP

Mastering Php

Uploaded by

Hacky Hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
513 views18 pages

Mastering PHP

Mastering Php

Uploaded by

Hacky Hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Mastering PHP

Best Practices for Modern PHP Development


Sergey Popoff and John Matthews
This book is for sale at http://leanpub.com/masteringphp
This version was published on 2015-10-13

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
2015 Sergey Popoff and John Matthews

Also By Sergey Popoff


PHP 7

Its a long way to the top if you want to rocknroll

Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CHAPTER 1. PHP THE RIGHT WAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CHAPTER 2. FRAMEWORKS . . . . . . . . . . . . . .
WHY USE ONE? . . . . . . . . . . . . . . . . . . . .
WHICH ONE TO USE? . . . . . . . . . . . . . . . .
WHAT IF I NEED TO MANTAIN EXISTING APPS?
HOW TO START . . . . . . . . . . . . . . . . . . .
HOW MANY FRAMEWORKS TO LEARN? . . . . .

.
.
.
.
.
.

5
5
5
5
6
6

CHAPTER 3. OOP & PATTERNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


WHY BOTHER? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7
7

CHAPTER 4. DEPENDENCY INJECTION & PSR


MVC . . . . . . . . . . . . . . . . . . . . . . .
DEPENDENCY INJECTION . . . . . . . . . .
PSRs . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

8
8
8
8

CHAPTER 5. COMPOSER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CHAPTER 6. VAGRANT & DOCKER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


VAGRANT AND DOCKER AND DIFFERENT BEASTS . . . . . . . . . . . . . . . . . .
WHY SHOULD I USE IT? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10
10
11

CHAPTER 7. IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

CHAPTER 8. PRACTICE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

INTRODUCTION
I am constantly asked what are some good PHP practices with modern PHP. This little book is
a collection of short and straightforward answers to many of these frequent questions.
Learning PHP can be a daunting task, but reading this book is a step in the right direction as the
following chapters will help you to better understand the accepted best-practices employed by PHP
professionals around the world.
So good luck, and happy reading!

GETTING STARTED
It is important to note that this book will focus on software engineer and computer science as a
whole, not just PHP development. This is because modern PHP programming is more than running
a homepage with 10 unique visitors by month.
One of the best sources online for PHP is still php.net. Read it carefully and thoroughly, as there is
a great wealth of information that youll benefit from. Its also free, which is a nice touch.
With that said, lets move on to the topics that were going to be discussing in this book.

CONTENTS
1.
2.
3.
4.
5.
6.
7.
8.

Read PHP: The Right Way


Learn a framework (any framework)
Learn about OOP, patterns
Learn about MVC, DI and PSR
Learn about Composer
Learn about Vargant & Docker
Use an IDE and debugger
Practice

CHAPTER 1. PHP THE RIGHT WAY


PHP: The Right Way (www.phptherightway.com) is an easy-to-read, quick reference guide for PHP
best practices, accepted coding standards, and links to the best PHP tutorials on the Web.
Its all pretty thorough, so I wont reiterate any of it here.
Its also a good idea to take a look at the latest features of PHP. From PHP4 to PHP 5, a solid
object-oriented model was written into the code (2004), PHP5.3 added anonymous functions and
namespaces (2009), and PHP5.4 added traits (2012).
It is better to learn the most up-to-date version PHP; still, using most up-to-date release (even stable)
in a production environment maybe not a best idea.
If you want to be ahead the competition, then Ive collected several best resources on learning
upcoming PHP 7. Take a look at php7start.tk for further information on PHPs latest build.

CHAPTER 2. FRAMEWORKS
WHY USE ONE?
By using a framework you can jump start the production process and reduce the amount of code
youll need to write from the get-go. Theres no need to re-invent the wheel, so go ahead and take
advantage of a pre-built framework.
Using a framework will also help you out if youre new to programming. They often come built
with best standards in mind and their communities are often ready to lend a helping hand if you
get stuck.
Further Reading: Why use a framework

WHICH ONE TO USE?


Its one of those things that you wont know which one will suit your needs until you give them a
try. Opinions vary wildly, and in 95% of cases it really doesnt matter which you choose, as long as
its in active development and widely used. Its better to choose one of the most popular frameworks
as itll likely have better documentation and a more active community. My personal preference is
Laravel at the moment.
Laravel uses Symfony components and as such borrows fairly heavily from the Symfony
library.
Zend Framework is solid, but not so popular as it once was.
Yii is a Laravel alternative.
The Slim Framework and Silex Framework are both great for learning.

WHAT IF I NEED TO MANTAIN EXISTING APPS?


When picking a framework you need to think about whether youll maintaining existing apps or
work on new ones. I would recommend having a goal of learning more than one framework, but if
you start with Laravel and you get familiar with the general concepts (not just the Laravel-specific
tools) then those concepts will carry over to Codeigniter, Zend Framework, Symfony, and even other
languages.

CHAPTER 2. FRAMEWORKS

HOW TO START
Whichever framework you choose, heres a couple of tips for getting the hang of things quickly.
Start with the official documentation and tutorials, youll learn a lot of useful info from that
they can give you.
Github also holds a wealth of information, so find 4 or 5 of the most popular projects built
with the framework you are learning, do a git clone and take a peak at them. Examples and
tutorials are great, but nothing beats real-world examples. Take a look at what other people
are working on and how theyre going about doing it.

HOW MANY FRAMEWORKS TO LEARN?


Be a PHP programmer who can program in whatever framework you or your employer decides to
use.

CHAPTER 3. OOP & PATTERNS


OOP stands for Object-oriented programming.
Matt Zandstras PHP Objects, Patterns and Practice gives a great introduction in PHP based OOP and
I strongly recommend having a look at it if you dont have a huge amount of practical programming
experience under your belt.
If you have enough time, you might also want to consider reading classics Design Patterns: Elements
of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John
Vlissides. Its quite a tricky read, so if youd fancy something a bit easier then give Head First Design
Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates and Kathy Sierra a try. Also, dont worry
that the books dont focus too heavily on PHP as programming patterns are generally independent
of a specific language.
Keep these three steps in mind while learning these patterns:
1. You dont know about them and dont use them;
2. You know about them and use them everywhere;
3. You know about them and use them only where you really need them.
Learning how and when to use patterns is a key to reducing complexity of your software. They
reduce the cost of change drastically and help to build large, successful, and maintainable web
applications. I strongly recommend becoming familiar with as many as you can before moving
forward.

WHY BOTHER?
If youre not aware of concepts like Model-View-Controller (MVC), Object-relational mapping
(ORM), Doctrine Database Abstraction Layer (DBAL), or abstraction then learning a framework
will help you out an incredibly amount, especially if youre working on your own.

CHAPTER 4. DEPENDENCY INJECTION


& PSR
MVC
Model-View-Controller (or MVC for short) is a software architecture that is used in software
engineering where the fundamental principle is based on the idea that the logic of an application
should be separated from its presentation. Put simply, I would say that MVC is simply a better way
of separating the logic of your application from the display.
The MVC principle is to separate the application into 3 main parts, known as the Model, the View,
and the Controller.
Further Reading: any frameworks tutorial first chapter, and the MVC in PHP article series.

DEPENDENCY INJECTION
Dependency Injection (DI) is providing a component with its dependencies either through constructor injection, method calls or the setting of properties. If that sounds daunting then believe me that
its much simpler than it sounds.
Further Reading: Anthony Ferraras What is dependency injection video the complementing
articles on the subject are great if youre looking for a deeper understanding into DI.

PSRs
PHP Standard Recommendations (PSR) are essentially the recommendation of how things should
be done, straight from the PHP community.
Different PSRs are explained on http://www.php-fig.org/. Please consider reading it and using as a
part of your codestyle.

CHAPTER 5. COMPOSER
Composer is a tool for dependency management in PHP. Its super useful and I suggest you take a
look into it.
Further Reading: There are a lot of good tutorials on Composer but these two stick out as being the
most useful. Composer Namespaces In 5 Minutes and screencasts on KNPUniversity.

CHAPTER 6. VAGRANT & DOCKER


Software engineering is often difficult because programmers have to deal with inconsistent environments for development and for production execution of their products. Due to mismatches between
these environments, developers often find bugs that surface in one environment but not in another.
Hardware based virtualization (VMWare, HyperV, etc) helps with the inconsistency issue because
it enables developers to create dev/test environments that could later be replicated into production.
However this category of virtualization requires more computational resources (especially storage)
than operating system virtualization like LXC used by Docker.
Docker and Vagrant are two efficient solutions that take different roads to solving the limitations of
the traditional virtual machine like VMware or VirtualBox.

VAGRANT AND DOCKER AND DIFFERENT BEASTS


Docker (http://docker.com/) is a two part shell/management layer for building and running
virtual linux containers, based on LXC. The great thing about Docker is that it is light-weight
(because it relies on shared-kernel linux containers) and it is distribution agnostic. While the
kernel between all instances is shared (but isolated from the host and each other), the user
space for different instances can be based on different linux distributions.
Vagrant (http://vagrantup.com/) on the other hand is a wonderful tool for automatically
provisioning multiple virtual machines each with their own configurations managed with
puppet and/or chef. For its virtualisation it can use different providers. Originally the default
provider was virtualbox, but it now supports many more, including vmware fusion and even
amazon-ec2.
In addition to requiring less resources than hardware virtualization, Docker defines a convenient
container specification format (Dockerfile) and a way to share these specifications (DockerHub).
When used together, these Docker technologies accelerate the process of defining a consistent
environment for both development and production in your application. Dockerfiles are easy to
maintain and help reduce the need for a dedicated operations team for your application. Docker,
by the virtue of relying on a thinner virtualization layer than hardware hypervisors like VMWare,
also has higher performance for I/O intensive operations. When used on bare metal hardware, you
going to be able to get better performance for many databases in Docker containers compared to
databases running in a virtual machine guest.
In buzzword speak, your team can become more DevOps.

CHAPTER 6. VAGRANT & DOCKER

11

WHY SHOULD I USE IT?


Docker can help you to:

maintain consistent developer/testing/production environments


use less resources than hardware virtualization
free up the time your team spends on dev to ops handoffs
improve your app I/O performance compared to running in a hardware virtualization based
virtual machine guest

CHAPTER 7. IDE
I strongly recommend using modern IDE like PHPStorm, Sublime Text (with plugins) or Atom (with
plugins).
PHPStorm is a complex IDE with everything included, but after learning about its features and
shortkeys it is a real timesaver. Dont waste your time, download free EAP version and try it out.
Youll either love it or hate it, but it is part of the modern workflow for many developers over the
world.
Also dont forget about using a debugger like XDebug.
But if youre still attached to your terminal then theres always VIM and EMACS, but you knew
that already.

CHAPTER 8. PRACTICE
Here is a list of different ideas that will help you to master your PHP programming skills.
Reading code is great to learn more about a language
Contributing to an open source PHP project is a great way to learn a lot and also will look
great on your resume
Learning another language will vastly improve your PHP skills
PHP interpreter and extensions are written in C, so learning C will be helpful when you decide
to dive deeper into the world of programming
Learning PHP 7 and future versions will let you get the best performance from your apps. You
can get started on http://php7start.tk/

You might also like