Mastering PHP
Mastering PHP
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
Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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
7
7
.
.
.
.
8
8
8
8
CHAPTER 5. COMPOSER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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.
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
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.
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.
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.
11
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/