Christopher Pitt
Verbena Close 1, Stellenberg, Durbanville, Cape Town, South Africa
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484269565. For more detailed information, please visit http://www.apress.com/source-code.
ISBN 978-1-4842-6956-5 e-ISBN 978-1-4842-6957-2
https://doi.org/10.1007/978-1-4842-6957-2
Christopher Pitt 2021
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.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Before We Start
So much has happened in the years since I first wrote about this topic. The language has evolved to be useful in more contexts. Package management is a solved problem. Frameworks like Laravel and Symfony are mature and widely used.
When I think back to what PHP development was like, eight or ten years ago, I am amazed by where we find ourselves. It is easier than ever to get started and to make a living building applications using PHP.
Topics Well Cover
This is a revised edition of the book I wrote in 2012. Were going to cover some of the same topics, but everything has been rewritten from the ground up. Heres a list of things well cover:
Getting PHP set up on our computer
Writing our first bit of framework code
Mapping different URLs to different scripts
Making HTML templates
Adding validation
Connecting to the database
Loading database data into objects
Testing our code
Finding and sharing objects in different places
Laying the foundation for extension
Adding sessions, cache, filesystems, queues, logging, and mail
Putting our code on GitHub and Composer
Each new section builds on the same application and prior knowledge, so that more complex topics stay manageable. The previous edition was depressingly academic, and my goal for this edition is to make it as practical and useful as possible.
Well be using features new to PHP 8 and diving deep into how popular frameworks implement the same features we do.
What Is MVC
A question occurs to me as I begin writing this revision.
Do we still need MVC?
The term was coined in the 1970s by Trygve Reenskaug . It jumped over to web development where frameworks like Django and Rails embraced it.
At the core, it is about separating parts of an application into
The data and business rules, called the Model
The presentation, usually of HTML, called the View
The connecting layer between those two, called the Controller
While it is true that modern web development is a very different environment to where MVC was first thought up, it remains a useful separation of the concerns of an application.
In fact, its almost impossible not to see these divisions when talking about or building a nontrivial PHP application. As we talk about building modern applications, well see bits from many frameworks, and well learn what they have to do with this separation.
What Our Focus Will Be
I want us to focus on the core elements of building a web-accessible application in PHP. Well start with accepting a request from the browser and serving a response.
Everything else should build upon this process.
As we continue, well look for ways to add more to this process. As such, I encourage you to follow along with the example application. Well be building a website for a new space exploration company, called Whoosh.
Whoosh needs a website to sell their space rockets and recruit new astronauts to their cause. Well start simple, but by the end, Whoosh will have a website that looks great and works well!
Are We Writing Production Code?
Were going to write secure and bug-free code, to the best of our knowledge. While it should be to the level that you can safely use the code we write in production, I dont recommend that you do.
Its probably a topic for another time and place, but there are many benefits to using established, mature PHP frameworks. When you use a mature framework, you automatically benefit from
Having many eyes on the code, to spot security issues early
Having many hands to help maintain and upgrade the code
Being able to hire new folks to work on your applications, who dont then also have to learn your proprietary code to be impactful
Writing your own framework is an excellent way to learn how other frameworks solve similar problems, and it should also be an opportunity to reflect on how much work and pressure popular frameworks can take off your hands.
You get to decide how much of your own code you want to write, but it would be foolish to use 100% of your own code in production.
Ways to Learn PHP
There are many ways to learn PHP. This book will teach things that may be unfamiliar to a new developer, but the goal isnt to be the best place for someone to learn programming (or even PHP) for the first time.
You should be familiar with basic PHP syntax before reading this book. Some topics include
Using variables, loops, and control flow statements
Using classes and creating new objects
Loading different scripts, using require, include, and so on
Using the command line for common, repeated tasks, like installing new Composer libraries