PHP 8 Objects, Patterns, and Practice Volume 2 (Zandstra M.) (z-library.sk, 1lib.sk, z-lib.sk)
Author: Zandstra M.
PHP
No Description
📄 File Format:
PDF
💾 File Size:
10.4 MB
13
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
PHP 8 Objects, Patterns, and Practice: Volume 2 Mastering Essential Development Tools — Seventh Edition — Matt Zandstra
📄 Page
2
PHP 8 Objects, Patterns, and Practice: Volume 2 Mastering Essential Development Tools Seventh Edition Matt Zandstra
📄 Page
3
PHP 8 Objects, Patterns, and Practice: Volume 2: Mastering Essential Development Tools, Seventh Edition ISBN-13 (pbk): 979-8-8688-0778-7 ISBN-13 (electronic): 979-8-8688-0779-4 https://doi.org/10.1007/979-8-8688-0779-4 Copyright © 2025 by Matt Zandstra This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: James Robinson Prior Editorial Assistant: Jacob Shmulewitz Cover designed by eStudioCalamar Cover image designed by Pawel Czerwinski on Unsplash Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a Delaware LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit https://www.apress.com/gp/services/ source-code. If disposing of this product, please recycle the paper Matt Zandstra Brighton, UK
📄 Page
4
To Louise. Still the whole point.
📄 Page
5
v Table of Contents About the Author ��������������������������������������������������������������������������������������������������� xiii Acknowledgments ���������������������������������������������������������������������������������������������������xv Introduction �����������������������������������������������������������������������������������������������������������xvii Chapter 1: Good (and Bad) Practice ������������������������������������������������������������������������� 1 Beyond Code ��������������������������������������������������������������������������������������������������������������������������������� 2 Borrowing a Wheel ������������������������������������������������������������������������������������������������������������������������ 2 Playing Nice ���������������������������������������������������������������������������������������������������������������������������������� 5 Giving Your Code Wings����������������������������������������������������������������������������������������������������������������� 6 Standards �������������������������������������������������������������������������������������������������������������������������������������� 7 Vagrant and Docker����������������������������������������������������������������������������������������������������������������������� 8 Testing������������������������������������������������������������������������������������������������������������������������������������������� 8 Command-Line Scripting ������������������������������������������������������������������������������������������������������������ 10 Continuous Integration ���������������������������������������������������������������������������������������������������������������� 10 Summary������������������������������������������������������������������������������������������������������������������������������������� 11 Chapter 2: Generating Documentation with phpDocumentor ��������������������������������� 13 Why Document? �������������������������������������������������������������������������������������������������������������������������� 14 Installation ���������������������������������������������������������������������������������������������������������������������������������� 15 Generating Documentation ��������������������������������������������������������������������������������������������������������� 15 DocBlock Comments ������������������������������������������������������������������������������������������������������������������� 18 Documenting Classes ����������������������������������������������������������������������������������������������������������������� 20 File-Level Documentation ����������������������������������������������������������������������������������������������������������� 21 Documenting Properties ������������������������������������������������������������������������������������������������������������� 22
📄 Page
6
vi Documenting Methods ���������������������������������������������������������������������������������������������������������������� 23 Creating Links in Documentation ������������������������������������������������������������������������������������������������ 25 Summary������������������������������������������������������������������������������������������������������������������������������������� 29 Chapter 3: PHP Standards �������������������������������������������������������������������������������������� 31 Why Standards? �������������������������������������������������������������������������������������������������������������������������� 31 What Are PHP Standards Recommendations? ���������������������������������������������������������������������������� 32 Why PSR in Particular? ���������������������������������������������������������������������������������������������������������� 34 Who Are PSRs For? ���������������������������������������������������������������������������������������������������������������� 34 Coding with Style ������������������������������������������������������������������������������������������������������������������������ 35 PSR-1 Basic Coding Standard ����������������������������������������������������������������������������������������������� 36 PSR-12 Extended Coding Style ���������������������������������������������������������������������������������������������� 39 PSR-4 Autoloading ���������������������������������������������������������������������������������������������������������������������� 47 The Rules That Matter to Us �������������������������������������������������������������������������������������������������� 47 PSR-11 Container Interface �������������������������������������������������������������������������������������������������������� 51 Summary������������������������������������������������������������������������������������������������������������������������������������� 53 Chapter 4: Refactoring and Standards Tools ���������������������������������������������������������� 55 PHP_CodeSniffer ������������������������������������������������������������������������������������������������������������������������� 56 Checking and Fixing Your Code ��������������������������������������������������������������������������������������������� 56 Managing the Scope of an Analysis��������������������������������������������������������������������������������������� 59 Creating Your Own Sniff ��������������������������������������������������������������������������������������������������������� 61 PHPStan �������������������������������������������������������������������������������������������������������������������������������������� 69 Installing PHPStan ����������������������������������������������������������������������������������������������������������������� 69 Running PHPStan������������������������������������������������������������������������������������������������������������������� 69 Rule Levels ���������������������������������������������������������������������������������������������������������������������������� 70 Telling PHPStan to Ignore Errors �������������������������������������������������������������������������������������������� 72 Array Arguments: Correcting Outside the Language ������������������������������������������������������������������� 74 Summary������������������������������������������������������������������������������������������������������������������������������������� 76 Table of ConTenTs
📄 Page
7
vii Chapter 5: Using and Creating Components with Composer ���������������������������������� 77 What Is Composer? ��������������������������������������������������������������������������������������������������������������������� 78 Installing Composer �������������������������������������������������������������������������������������������������������������������� 78 Installing a (Set of) Package(s) ��������������������������������������������������������������������������������������������������� 79 Installing a Package from the Command Line ����������������������������������������������������������������������� 80 Versions ��������������������������������������������������������������������������������������������������������������������������������� 81 require-dev ���������������������������������������������������������������������������������������������������������������������������� 83 Composer and Autoload �������������������������������������������������������������������������������������������������������������� 85 Creating Your Own Package �������������������������������������������������������������������������������������������������������� 86 Adding Package Information ������������������������������������������������������������������������������������������������� 86 Platform Packages ���������������������������������������������������������������������������������������������������������������� 87 Distribution Through Packagist ��������������������������������������������������������������������������������������������������� 88 Keeping It Private ������������������������������������������������������������������������������������������������������������������������ 92 Summary������������������������������������������������������������������������������������������������������������������������������������� 94 Chapter 6: Version Control with Git ������������������������������������������������������������������������ 95 Why Use Version Control? ����������������������������������������������������������������������������������������������������������� 95 Getting Git ����������������������������������������������������������������������������������������������������������������������������������� 97 Using an Online Git Repository ���������������������������������������������������������������������������������������������������� 98 Configuring a Git Server ������������������������������������������������������������������������������������������������������������ 100 Creating the Remote Repository ������������������������������������������������������������������������������������������ 101 Beginning a Project ������������������������������������������������������������������������������������������������������������������� 103 Cloning the Repository �������������������������������������������������������������������������������������������������������� 107 Updating and Committing ��������������������������������������������������������������������������������������������������������� 108 Adding and Removing Files and Directories ����������������������������������������������������������������������������� 113 Adding a File ������������������������������������������������������������������������������������������������������������������������ 113 Removing a File ������������������������������������������������������������������������������������������������������������������� 114 Adding a Directory ��������������������������������������������������������������������������������������������������������������� 114 Removing Directories ���������������������������������������������������������������������������������������������������������� 115 Renaming Files or Directories ��������������������������������������������������������������������������������������������� 115 Table of ConTenTs
📄 Page
8
viii Tagging a Release ��������������������������������������������������������������������������������������������������������������������� 115 Branching a Project ������������������������������������������������������������������������������������������������������������������� 117 Summary����������������������������������������������������������������������������������������������������������������������������������� 128 Chapter 7: Testing with PHPUnit ��������������������������������������������������������������������������� 129 Functional Tests and Unit Tests ������������������������������������������������������������������������������������������������� 130 Testing by Hand ������������������������������������������������������������������������������������������������������������������������� 131 Introducing PHPUnit ������������������������������������������������������������������������������������������������������������������ 134 Creating a Test Case ������������������������������������������������������������������������������������������������������������ 135 Assertion Methods ��������������������������������������������������������������������������������������������������������������� 139 Testing Exceptions ��������������������������������������������������������������������������������������������������������������� 140 Running Test Suites ������������������������������������������������������������������������������������������������������������� 142 Constraints �������������������������������������������������������������������������������������������������������������������������� 143 Mocks and Stubs ����������������������������������������������������������������������������������������������������������������� 146 Tests Succeed When They Fail �������������������������������������������������������������������������������������������� 150 Writing Web Tests ���������������������������������������������������������������������������������������������������������������������� 156 Introducing Selenium ���������������������������������������������������������������������������������������������������������� 157 A Note of Caution ���������������������������������������������������������������������������������������������������������������������� 167 Summary����������������������������������������������������������������������������������������������������������������������������������� 169 Chapter 8: Vagrant ������������������������������������������������������������������������������������������������ 171 The Problem ������������������������������������������������������������������������������������������������������������������������������ 171 A Little Setup ����������������������������������������������������������������������������������������������������������������������������� 173 Choosing and Installing a Vagrant Box �������������������������������������������������������������������������������� 173 Mounting Local Directories on the Vagrant Box ������������������������������������������������������������������������ 176 Provisioning ������������������������������������������������������������������������������������������������������������������������������ 179 Setting Up the Web Server �������������������������������������������������������������������������������������������������� 181 Setting Up MariaDB ������������������������������������������������������������������������������������������������������������� 182 Configuring a Hostname ������������������������������������������������������������������������������������������������������ 183 Wrapping It Up �������������������������������������������������������������������������������������������������������������������������� 185 Summary����������������������������������������������������������������������������������������������������������������������������������� 187 Table of ConTenTs
📄 Page
9
ix Chapter 9: Docker ������������������������������������������������������������������������������������������������� 189 What Is Docker? ������������������������������������������������������������������������������������������������������������������������ 190 Getting Docker �������������������������������������������������������������������������������������������������������������������������� 190 Running an Image ��������������������������������������������������������������������������������������������������������������������� 192 Establishing Some Docker Terms ���������������������������������������������������������������������������������������������� 193 Acquiring an Image with docker pull ���������������������������������������������������������������������������������������� 194 Creating and Invoking a Container with docker run ������������������������������������������������������������������ 195 Listing Containers ��������������������������������������������������������������������������������������������������������������������� 196 Accessing a Container with docker run ������������������������������������������������������������������������������������ 197 Running a Container in the Background ����������������������������������������������������������������������������������� 197 Accessing a Container with docker exec ���������������������������������������������������������������������������������� 198 Building Your Own Image ���������������������������������������������������������������������������������������������������������� 199 In the Weeds with CMD and ENTRYPOINT ��������������������������������������������������������������������������� 201 Mounting a Local Directory ������������������������������������������������������������������������������������������������������� 204 A Single Command Development Environment������������������������������������������������������������������������� 205 Building a System Out of Multiple Containers��������������������������������������������������������������������������� 206 Removing Images and Containers �������������������������������������������������������������������������������������������� 208 Creating and Using a Named Bridge Network �������������������������������������������������������������������������� 210 Docker Compose ����������������������������������������������������������������������������������������������������������������������� 213 Resetting the Project ����������������������������������������������������������������������������������������������������������� 214 The Compose File ���������������������������������������������������������������������������������������������������������������� 214 Combining Docker Compose and Dockerfile ����������������������������������������������������������������������� 216 Adding a Second Service ���������������������������������������������������������������������������������������������������������� 217 What About Composer? ������������������������������������������������������������������������������������������������������� 219 Some Docker Compose Commands ������������������������������������������������������������������������������������ 221 Summary����������������������������������������������������������������������������������������������������������������������������������� 222 Chapter 10: Automating Build and Deployment with Ansible ������������������������������� 223 What Is Ansible? ������������������������������������������������������������������������������������������������������������������������ 224 Getting Ansible �������������������������������������������������������������������������������������������������������������������������� 224 Confirming Your Install �������������������������������������������������������������������������������������������������������������� 225 Table of ConTenTs
📄 Page
10
x Command-Line Utilities ������������������������������������������������������������������������������������������������������������� 226 Hello, Ansible ����������������������������������������������������������������������������������������������������������������������������� 227 Inventories: Working with Hosts ������������������������������������������������������������������������������������������������ 229 Checking Out a Git Repository �������������������������������������������������������������������������������������������������� 234 Copying a Configuration File ����������������������������������������������������������������������������������������������������� 235 Some More on Variables ����������������������������������������������������������������������������������������������������������� 236 Declaring Variables with vars ���������������������������������������������������������������������������������������������� 236 Overriding Variables from the Command Line ��������������������������������������������������������������������� 237 Placing Variables in Files ����������������������������������������������������������������������������������������������������� 238 Interpolating Values into a File �������������������������������������������������������������������������������������������������� 239 Managing Secrets with Ansible Vault ���������������������������������������������������������������������������������������� 241 Checking in on Megaquiz ���������������������������������������������������������������������������������������������������������� 243 Inventory Variables �������������������������������������������������������������������������������������������������������������������� 246 The Composer Module �������������������������������������������������������������������������������������������������������������� 248 Conditionals ������������������������������������������������������������������������������������������������������������������������������ 249 Summary����������������������������������������������������������������������������������������������������������������������������������� 250 Chapter 11: PHP on the Command Line ���������������������������������������������������������������� 251 Why the Command Line? ���������������������������������������������������������������������������������������������������������� 252 A Dummy Function �������������������������������������������������������������������������������������������������������������������� 253 Autoloading ������������������������������������������������������������������������������������������������������������������������������� 254 Acquiring Arguments ���������������������������������������������������������������������������������������������������������������� 255 The Shebang ����������������������������������������������������������������������������������������������������������������������������� 256 Error Conditions ������������������������������������������������������������������������������������������������������������������������ 257 Usage ���������������������������������������������������������������������������������������������������������������������������������������� 258 Handling Arguments and Options���������������������������������������������������������������������������������������������� 260 Options �������������������������������������������������������������������������������������������������������������������������������� 260 Introducing getopt ��������������������������������������������������������������������������������������������������������������� 262 The Problem with getopt() ��������������������������������������������������������������������������������������������������� 263 Using GetOpt�php ����������������������������������������������������������������������������������������������������������������� 263 Enforcing Positional Arguments ������������������������������������������������������������������������������������������ 268 Table of ConTenTs
📄 Page
11
xi Handling Output ������������������������������������������������������������������������������������������������������������������������ 269 Updating the Example Script����������������������������������������������������������������������������������������������������� 271 Adding Verbose Mode ���������������������������������������������������������������������������������������������������������� 273 Prompted Input ������������������������������������������������������������������������������������������������������������������������� 275 Piped Input �������������������������������������������������������������������������������������������������������������������������� 276 Packaging Up ���������������������������������������������������������������������������������������������������������������������������� 277 Distribution with Composer ������������������������������������������������������������������������������������������������� 278 Creating a Phar �������������������������������������������������������������������������������������������������������������������� 281 Executing Shell Commands ������������������������������������������������������������������������������������������������������ 283 Summary����������������������������������������������������������������������������������������������������������������������������������� 286 Chapter 12: Continuous Integration ���������������������������������������������������������������������� 287 What Is Continuous Integration? ����������������������������������������������������������������������������������������������� 288 Preparing a Project for CI ���������������������������������������������������������������������������������������������������� 290 Getting and Installing Jenkins ��������������������������������������������������������������������������������������������������� 293 Installing Jenkins ���������������������������������������������������������������������������������������������������������������� 294 Installing Jenkins Plug-ins �������������������������������������������������������������������������������������������������� 297 Setting Up Git in Jenkins ����������������������������������������������������������������������������������������������������� 298 Configuring Composer and PHPUnit ������������������������������������������������������������������������������������ 302 Running the First Build �������������������������������������������������������������������������������������������������������� 304 Triggering Builds������������������������������������������������������������������������������������������������������������������ 304 A Jenkins Agent ������������������������������������������������������������������������������������������������������������������� 308 GitHub Actions ��������������������������������������������������������������������������������������������������������������������������� 315 Why GitHub Actions? ����������������������������������������������������������������������������������������������������������� 316 The Basics ��������������������������������������������������������������������������������������������������������������������������� 316 Checking Out the Code �������������������������������������������������������������������������������������������������������� 320 Running Composer �������������������������������������������������������������������������������������������������������������� 321 Running PHPUnit ����������������������������������������������������������������������������������������������������������������� 323 What Next? �������������������������������������������������������������������������������������������������������������������������� 324 Summary ����������������������������������������������������������������������������������������������������������������������������� 325 Table of ConTenTs
📄 Page
12
xii Chapter 13: PHP Practice ������������������������������������������������������������������������������������� 327 Practice ������������������������������������������������������������������������������������������������������������������������������������� 328 Testing ��������������������������������������������������������������������������������������������������������������������������������� 329 Standards and Standards Tools ������������������������������������������������������������������������������������������� 329 Inline Documentation����������������������������������������������������������������������������������������������������������� 330 Development Environments ������������������������������������������������������������������������������������������������� 330 Version Control �������������������������������������������������������������������������������������������������������������������� 331 Build and Deployment ��������������������������������������������������������������������������������������������������������� 331 Command-Line Scripting ����������������������������������������������������������������������������������������������������� 332 Continuous Integration �������������������������������������������������������������������������������������������������������� 332 What I Missed ���������������������������������������������������������������������������������������������������������������������� 333 Summary����������������������������������������������������������������������������������������������������������������������������������� 335 Bibliography ��������������������������������������������������������������������������������������������������������� 337 Index ��������������������������������������������������������������������������������������������������������������������� 341 Table of ConTenTs
📄 Page
13
xiii About the Author Matt Zandstra has worked as a web programmer, consultant, and writer for over two decades. In addition to this book, he is the author of Sams Teach Yourself PHP in 24 Hours (three editions) and a contributor to DHTML Unleashed. He has written articles for Linux Magazine, Zend, IBM DeveloperWorks, and PHP Architect magazine and also writes fiction. Matt was a senior developer/tech lead at Yahoo and API tech lead at LoveCrafts. He now runs an agency that advises companies on their architectures and system management and develops systems primarily with PHP, Python, and Java.
📄 Page
14
xv Acknowledgments I have benefited from the support of many people while working on this edition. But as always, I must also look back to the book’s origins. I tried out some of this book’s underlying concepts in a talk in Brighton, back when we were all first marveling at the shiny possibilities of PHP 5. Thanks to Andy Budd, who hosted the talk, and to the vibrant Brighton developer community. Thanks also to Jessey White-Cinis, who was at that meeting and who put me in touch with Martin Streicher at Apress. Once again, this time around, the Apress team has provided enormous support, feedback, and encouragement. I am lucky to have benefited from such professionalism. I'm delighted that my friend and colleague, Paul Tregoing, agreed again to act as technical reviewer despite many other projects including his own book. This edition has greatly benefited from Paul’s knowledge, insight, and attention to detail – many thanks Paul! Thanks and love to my wife, Louise. The production of this book has coincided with the university careers of my children Holly and Viola who have struggled with their own deadlines and creative blocks. Thanks are due to them for keeping me company at the kitchen table as we found our separate ways together! I write to music, and in previous editions of this book, I remembered the great DJ, John Peel, champion of the underground and the eclectic. The soundtrack for this edition was largely provided by BBC Radio 3’s Late Junction and Six Music's Freak Zone both played on a loop. Thanks to the DJs and musicians who continue to keep things weird.
📄 Page
15
xvii Introduction When I decided to learn to program, I went out to a bookshop on Tottenham Court Road in London and bought myself a book about Perl. Excited, I started building an application before I'd even finished the fourth chapter, which is how I managed to write a working forum application without yet knowing how to define a subroutine. That's another story, though (one involving very very big loops). Once I had finished reading the book and rounded out my understanding, I felt sure I had learned everything I needed to know. I was ready. It was only then that I began to perceive new gaps in my knowledge. Some of it was relatively easy to fix. I was able to find books on the Unix shell and CGI to address the most obvious chasm. But, even after that, I had questions. Where would I store my code? How would I collaborate with other developers without overwriting their work or having my own work clobbered? How should I source libraries and manage dependencies? What about development environments? What was the best way to deploy my code? How could I test the systems I built? The answers could be found online – though search in those days was rudimentary. I spent a lot of time on the Usenet search engine DejaNews and pieced together a working practice. In retrospect, it was somewhat suboptimal in all sorts of ways, but it was enough to help me get systems into the world. Over the years, I joined teams and learned from knowledgeable people. I searched out more books. My practice improved. The coverage gap was not the fault of that Perl book's author. He did a brilliant job within the book's remit. But when I came to pitch a book about coding with objects in PHP, I thought about the extent of that remit. Although I wanted to write about objects and design, I did not want to do so in an absolute vacuum. I wanted to write a practical book – a book that helped with the last yard of development too. So I added practice to my PHP, objects, and patterns proposal.
📄 Page
16
xviii PHP 8 Objects, Patterns, and Practice has evolved over the years and grown from a slim volume to a full on doorstopper. When the time came for a seventh edition, I had additions in mind as usual. As well as revising the existing topics covered by the Practice section, I wanted to include more on continuous integration, to add new subjects such as PHPStan, Docker, Ansible, and command-line PHP scripting. Of course, that was impossible. The sixth edition was huge. There was no way we could create a seventh edition that was even larger. Unless, of course, we broke the book into two volumes. So that is what we did. I hope you enjoy the result. InTroduCTIon
📄 Page
17
1 © Matt Zandstra 2025 M. Zandstra, PHP 8 Objects, Patterns, and Practice: Volume 2, https://doi.org/10.1007/979-8-8688-0779-4_1 CHAPTER 1 Good (and Bad) Practice In the previous volume, I focused on coding, concentrating particularly on the role of design in building flexible and reusable tools and applications. Development doesn’t end with code, however. It is possible to come away from books and courses with a solid understanding of a language, yet still encounter problems when it comes to running and deploying a project. In this volume, I will move beyond code to cover some of the tools and techniques that form the underpinnings of a successful development process. This chapter will cover the following: • Third-party packages: Where to get them and when to use them. • Deployment: Pushing your code across servers, applying configuration. • Version control: Bringing harmony to the development process. • Documentation: Writing code that is easy to understand, use, and extend. • Unit testing: A tool for automated bug detection and prevention. • Standards: Why it’s sometimes good to follow the herd. • Development environments: Every developer needs a lab of their own. A coder should be able to work safely with a system that resembles the production environment, no matter their hardware or OS. • Scripting the command line: PHP may be known as a web technology, but it can be just as powerful on the command line. • Continuous integration: Using this practice and set of tools to automate project builds and tests as well as to warn of problems as they occur.
📄 Page
18
2 Beyond Code When I first graduated from working on my own and took a place in a development team, I was astonished at how much stuff other developers seemed to have to know. Good-natured arguments simmered endlessly over issues of vital-seeming importance: Which is the best text editor? Should the team standardize on an integrated development environment? Should we impose a coding standard? How should we test our code? Should we document as we develop? Sometimes, these issues seemed more important than the code itself, and my colleagues seemed to have acquired their encyclopedic knowledge of the domain through some strange process of osmosis. The books I had read on PHP, Perl, and Java certainly didn’t stray from the code itself to any great extent. As I discussed in the previous volume, many books about programming rarely diverge from their tight focus on functions and syntax to take in code design. If design is off topic, you can be sure that wider issues such as version control and testing are rarely discussed. This is not a criticism – if a book professes to cover the main features of a language, it should be no surprise that this is principally what it does. In learning about code, however, I found that I had neglected many of the mechanics of a project’s day-to-day life. I discovered that some of these details were critical to the success or failure of projects I helped develop. In this chapter, and in more detail in coming chapters, I will look beyond code to explore some of the tools and techniques on which the success of your projects may depend. Borrowing a Wheel When faced with a challenging but discrete requirement in a project (the need to parse a particular format, perhaps, or to use a novel protocol in talking to a remote server), there is a lot to be said for building a component that addresses the need. It can also be one of the best ways to learn your craft. In creating a package, you gain insight into a problem and file away new techniques that might have wider application. You invest at once in your project and in your own skills. By keeping functionality internal to your system, you can save your users from having to download third-party packages. Occasionally, too, you may sidestep thorny licensing issues. There’s nothing like the sense of satisfaction you can get when you test a component you designed yourself and find that, wonder of wonders, it works – it does exactly what you wrote on the tin. Chapter 1 Good (and Bad) praCtiCe
📄 Page
19
3 There is a dark side to all this, of course. Many packages represent an investment of thousands of person-hours: a resource that you may not have on hand. You may be able to address this by developing only the functionality needed specifically by your project, whereas a third-party tool might fulfill a myriad of other needs as well. The question remains, however: If a freely available tool exists, why are you squandering your talents in reproducing it? Do you have the time and resources to develop, test, and debug your package? Might not this time be better deployed elsewhere? I am one of the worst offenders when it comes to wheel reinvention. Picking apart problems and inventing solutions to them is a fundamental part of what we do as coders. Getting down to some serious architecture is a more rewarding prospect than writing some glue to stitch together three or four existing components. When this temptation comes over me, I remind myself of projects past. Although the choice to build from scratch has never killed a project in my experience, I have seen it devour schedules and murder profit margins. There I sit with a manic gleam in my eye, hatching plots and spinning class diagrams, failing to notice as I obsess over the details of my component that the big picture is now a distant memory. Now, when I map out a project, I try to develop a feel for what belongs inside the code base and what should be treated as a third-party requirement. For example, your application may generate (or read) an RSS feed, and you may need to validate email addresses and automate mailouts, authenticate users, or read from a standard-format configuration file. All of these needs can be fulfilled by external packages. In previous versions of this book, I suggested that PEAR (PHP Extension and Application Repository) was the way to go for packages. Times change, though, and the PHP world has very definitely moved to the Composer dependency manager and its default repository, Packagist (https://packagist.org). Because Composer manages packages on a per-project basis, it is less prone to the dreaded dependency hell syndrome (where different packages require incompatible versions of the same library). Besides, the fact that all the action has moved to Composer/Packagist means that you’re more likely to find what you’re looking for there. What’s more, many of the PEAR packages are available through Packagist (https://packagist.org/packages/pear/). So, once you have defined your needs, your first stop should be the Packagist site. You can then use Composer to install your package and to manage package dependencies. I will cover Composer in more detail in Chapter 5. Chapter 1 Good (and Bad) praCtiCe
📄 Page
20
4 To give you some idea of what’s available using Composer and Packagist, here are just a few of the things you can do with the packages you’ll find there: • Cache output with pear/cache_lite • Test the efficiency of your code with the athletic/athletic benchmark library • Abstract the details of database access with doctrine/dbal • Extract RSS feeds with simplepie/simplepie • Access REST APIs with guzzlehttp/guzzle • Parse configuration file formats with symfony/config • Parse and manipulate URLs with league/uri The Packagist website provides a powerful search facility. You may find packages that address your needs there, or you may need to cast your net wider using a search engine. Either way, you should always take time to assess existing packages before setting out to potentially reinvent that wheel. The fact that you have a need – and that a package exists to address it – should not be the start and end of your deliberations. Although it is preferable to use a package where it will save you otherwise unnecessary development, in some cases, it can add overhead without real gain. You may find that a clean and focused class will get the job done without bloat or that PHP provides a decent built-in solution. Nonetheless, many programmers, myself included, often place too much emphasis on the creation of original code, sometimes to the detriment of their projects. Note the unwillingness to use third-party tools and solutions is often built-in at the institutional level. this tendency to treat external products with suspicion is sometimes known as the not invented here syndrome. as a further note, the technical reviewer and fellow sf fan paul tregoing points out that Not Invented Here is also the name of a ship in iain M. Banks’ Culture series. This emphasis on authorship may be one reason that there often seems to be more creation than actual use of reusable code. Chapter 1 Good (and Bad) praCtiCe
The above is a preview of the first 20 pages. Register to read the complete e-book.
Recommended for You
Loading recommended books...
Failed to load, please try again later