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.Mastering PHP Best Practices for Modern PHP Development Sergey Popoff and John Matthews This book is for sale at http://leanpub. 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 .

It’s a long way to the top if you want to rock’n’roll .

. . . . . . . . . . . . . . . . . . 5 5 5 5 6 6 CHAPTER 3. . . . . . . . . . . . . . . . . . . . . . . . . 4 CHAPTER 2. . . . . . . . . . . . . . . . . . . . . . . . 10 10 11 CHAPTER 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . PRACTICE . WHY SHOULD I USE IT? . . . . PHP THE RIGHT WAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DEPENDENCY INJECTION & PSR MVC . . . . . . . . . . . . . . . . WHY USE ONE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 7 CHAPTER 4. . . . . . 9 CHAPTER 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WHICH ONE TO USE? . . . . . . . . . . . . FRAMEWORKS . . . . . . . . . . . . IDE . . . . . . . . . . . . VAGRANT AND DOCKER AND DIFFERENT BEASTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . COMPOSER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WHAT IF I NEED TO MANTAIN EXISTING APPS? HOW TO START . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WHY BOTHER? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 CHAPTER 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DEPENDENCY INJECTION . . . . . . . . . 8 8 8 8 CHAPTER 5. . . . . HOW MANY FRAMEWORKS TO LEARN? . . . . . . . . 1 GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . PSRs . . . . . . . . . . . . . . . . . . . .Contents INTRODUCTION . . . . . . . . OOP & PATTERNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VAGRANT & DOCKER . . . . . . . . . . . . . . . . . . . . . . . 12 CHAPTER 8. . . . . . . . . . . . . . . . . .

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. So good luck. and happy reading! . 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.

With that said. . as there is a great wealth of information that you’ll benefit from. let’s move on to the topics that we’re going to be discussing in this book. not just PHP development. which is a nice touch. It’s also free. Read it carefully and thoroughly.net.GETTING STARTED It is important to note that this book will focus on software engineer and computer science as a whole. 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.

CONTENTS 1. 7. 2. 4. 3. 6. Read PHP: The Right Way Learn a framework (any framework) Learn about OOP. patterns Learn about MVC. 8. DI and PSR Learn about Composer Learn about Vargant & Docker Use an IDE and debugger Practice . 5.

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

• Yii is a Laravel alternative. and even other languages. Opinions vary wildly. There’s no need to re-invent the wheel. . Zend Framework. • Zend Framework is solid. and in 95% of cases it really doesn’t matter which you choose.CHAPTER 2. WHAT IF I NEED TO MANTAIN EXISTING APPS? When picking a framework you need to think about whether you’ll maintaining existing apps or work on new ones. Symfony. My personal preference is Laravel at the moment. as long as it’s in active development and widely used. It’s better to choose one of the most popular frameworks as it’ll likely have better documentation and a more active community. Using a framework will also help you out if you’re new to programming. • The Slim Framework and Silex Framework are both great for learning. Further Reading: Why use a framework WHICH ONE TO USE? It’s one of those things that you won’t know which one will suit your needs until you give them a try. • Laravel uses Symfony components and as such borrows fairly heavily from the Symfony library. but not so popular as it once was. FRAMEWORKS WHY USE ONE? By using a framework you can jump start the production process and reduce the amount of code you’ll need to write from the get-go. I would recommend having a goal of learning more than one framework. so go ahead and take advantage of a pre-built 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. They often come built with best standards in mind and their communities are often ready to lend a helping hand if you get stuck.

you’ll learn a lot of useful info from that they can give you. Take a look at what other people are working on and how they’re going about doing it. but nothing beats real-world examples. Examples and tutorials are great. do a git clone and take a peak at them. here’s a couple of tips for getting the hang of things quickly. HOW MANY FRAMEWORKS TO LEARN? Be a PHP programmer who can program in whatever framework you or your employer decides to use. .CHAPTER 2. FRAMEWORKS 6 HOW TO START Whichever framework you choose. • Start with the official documentation and tutorials. • Github also holds a wealth of information. so find 4 or 5 of the most popular projects built with the framework you are learning.

You know about them and use them everywhere. and maintainable web applications. you might also want to consider reading classics Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma. don’t worry that the books don’t focus too heavily on PHP as programming patterns are generally independent of a specific language. Doctrine Database Abstraction Layer (DBAL). 2. You don’t know about them and don’t use them. 3. especially if you’re working on your own. Object-relational mapping (ORM). They reduce the cost of change drastically and help to build large. It’s quite a tricky read. Matt Zandstra’s PHP Objects. WHY BOTHER? If you’re not aware of concepts like Model-View-Controller (MVC). Bert Bates and Kathy Sierra a try.CHAPTER 3. Keep these three steps in mind while learning these patterns: 1. Richard Helm. Learning how and when to use patterns is a key to reducing complexity of your software. so if you’d fancy something a bit easier then give Head First Design Patterns by Elisabeth Freeman. I strongly recommend becoming familiar with as many as you can before moving forward. successful. Also. If you have enough time. OOP & PATTERNS OOP stands for Object-oriented programming. You know about them and use them only where you really need them. Ralph Johnson and John Vlissides. Patterns and Practice gives a great introduction in PHP based OOP and I strongly recommend having a look at it if you don’t have a huge amount of practical programming experience under your belt. or abstraction then learning a framework will help you out an incredibly amount. . Eric Freeman.

and the Controller. straight from the PHP community. I would say that MVC is simply a better way of separating the logic of your application from the display. DEPENDENCY INJECTION Dependency Injection (DI) is providing a component with its dependencies either through constructor injection. Different PSRs are explained on http://www.php-fig. The MVC principle is to separate the application into 3 main parts. . If that sounds daunting then believe me that it’s much simpler than it sounds. method calls or the setting of properties. Put simply. Further Reading: any framework’s tutorial first chapter. 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. and the “MVC in PHP” article series. PSRs PHP Standard Recommendations (PSR) are essentially the recommendation of how things should be done.org/. Further Reading: Anthony Ferrara’s “What is dependency injection” video the complementing articles on the subject are great if you’re looking for a deeper understanding into DI. Please consider reading it and using as a part of your codestyle.CHAPTER 4. the View. known as the Model.

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

The great thing about Docker is that it is light-weight (because it relies on shared-kernel linux containers) and it is distribution agnostic.CHAPTER 6. . Docker defines a convenient container specification format (Dockerfile) and a way to share these specifications (DockerHub). by the virtue of relying on a thinner virtualization layer than hardware hypervisors like VMWare. For its virtualisation it can use different providers. While the kernel between all instances is shared (but isolated from the host and each other). including vmware fusion and even amazon-ec2. based on LXC. When used together. Docker and Vagrant are two efficient solutions that take different roads to solving the limitations of the traditional virtual machine like VMware or VirtualBox. but it now supports many more. In addition to requiring less resources than hardware virtualization. VAGRANT AND DOCKER AND DIFFERENT BEASTS • Docker (http://docker. developers often find bugs that surface in one environment but not in another. HyperV. these Docker technologies accelerate the process of defining a consistent environment for both development and production in your application. Docker. Due to mismatches between these environments. However this category of virtualization requires more computational resources (especially storage) than operating system virtualization like LXC used by Docker. Originally the default provider was virtualbox. your team can become more “DevOps”. Hardware based virtualization (VMWare. In buzzword speak. When used on bare metal hardware.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.com/) is a two part shell/management layer for building and running virtual linux containers. also has higher performance for I/O intensive operations. you going to be able to get better performance for many databases in Docker containers compared to databases running in a virtual machine guest. • Vagrant (http://vagrantup. etc) helps with the inconsistency issue because it enables developers to create dev/test environments that could later be replicated into production. the user space for different instances can be based on different linux distributions. Dockerfiles are easy to maintain and help reduce the need for a dedicated operations team for your application. VAGRANT & DOCKER Software engineering is often difficult because programmers have to deal with inconsistent environments for development and for production execution of their products.

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 .

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

tk/ . 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. You can get started on http://php7start. 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.CHAPTER 8.