Drupal Console
TopicDrupal Console provides a command line utility for performing common site administration tasks, code generation scaffolding, and a Read-Evaluate-Print-Loop for interacting with your Drupal site.
Git
TopicThe Git version control system can help you keep track of changes in your codebase and make sure you don't unintentionally lose work.
Drupal Composer Project
TopicThe Drupal Composer Project provides a scaffold for starting a new Drupal project and managing that project's dependencies with Composer. It was created before the drupal/recommended-project Composer project template was developed, which is the current best practice method for creating a new Drupal site with Composer.
Xdebug
TopicDebugging your Drupal codebase can be made substantially easier by learning how to enable and configure the Xdebug PHP extension.
Let's Write Some PHP
FreeSo you want to become a PHP developer. And not just any ole' programmer, but someone who writes great code and can work on exciting projects. Well then, this series from our partners at KnpUniversity is for you! You'll learn how to develop with PHP from the very beginning, with a real project, and coding exercises throughout the lessons so you can practice immediately.
In this series, we'll learn PHP from scratch by building a real website. This means you'll learn the practices used by developers to build really cool things, and not just a bunch of theory. We're going to build a site that we're calling AirPupnMeow.com. Imagine a site like Airbnb.com, except where people rent cute pets instead of apartments. If you're looking for companionship without all that responsibility of walking your dog every morning and bringing a bag to pick up his... uh gifts, then this site would be for you! Ok, the idea might be kinda silly, but that hasn't stopped startups in the past!
In this first lesson we are going to dive right in and create our first PHP file, play around with variables, and also look at what happens when we make an error, and how to read the error message we get. If you want to follow along with the steps here, you will need access to a web sever where you can write PHP files. You can get set up quickly with a number of local development servers. We have lessons that cover material for different operating systems in our Development Environments topic page.
Throughout this first series of PHP for Beginners, you are going to learn to:
- Use functions, and variables
- Work with arrays, loops, and if statements
- Read and update files
- Handle JSON
- Set up your computer to run PHP
- Create your own functions
- Organize your project into multiple files
- Create a simple layout
Additional resources
We already know what to write when we want to use some PHP code, how to set a variable, and how to print things. Like most languages, PHP also has functions that make it easier to do more complex tasks. Instead of you needing to figure out all of the logic to accomplish something, like randomizing a number, you can just use an existing PHP function, like rand(), to do the work for you. In this lesson, you'll learn how to use a function, what arguments are and how they work, and use some examples in our demo site code.
Additional resources
We've seen how we can create variables and set each to a string or a number. We've also used functions like rand and strtoupper
to return numbers and strings. Let's talk about a third type of variable in PHP: an array. An array represents a group of things, like 5 random numbers or 3 strings. They're a really important type of data in PHP, and you'll be using them often. In this lesson we'll create an array, and then use foreach to loop through the array so we can complete a repetitive action for each item in the array. We'll also look at how you can access a specific item in an array, explaining keys and indexes.
Additional resources
We have a basic array for our pets, but we can make these much more friendly by specifying our own array keys. We'll do just that, and we'll also see how to add new items to an already existing array, without having to go in and manually add it to the array in the code.
Additional resources
Every key in an array is either a string or a whole number, which we programmers and mathematicians call an integer. And that's the end of the story: array keys are only ever strings or integers in all of PHP. But each value in an array can be any type of PHP value. So far we know three data types in PHP: a string, a number and an array. This means that we can have multi-dimensional arrays: an array with another one inside of it. Multi-dimensional arrays are actually pretty common and easy to use. In this lesson we're going to expand our existing array by putting the pet details inside the array. Once we have the new data in the array we'll look at how we access this nested information, and how we can count what we have in there.
Additional resources
Right now our pet data array is just hardcoded and not dynamic. Eventually, we're going to pull this array from a database and let users add new pets to it. But before we get there, let's pretend someone has given us a file that contains all of adorable pets available on airPupnMeow.com. Our goal will be to read that file and turn its contents into a PHP array that looks just like the one we're creating now by hand. Once you add the sample JSON file to our project (found in the resources folder of the sample code), you'll learn how to open and read the file, and then decode the JSON into an array. You're also going to learn a little bit about debugging when working with files and file paths, and be introduced to PHP booleans.
When you reference a key on an array that doesn't exist, PHP will complain. Instead, let's code defensively. In other words, if we know that it's possible that the age key might be missing, we should check for it and only print the age if it's there. We need to add some logic. To do this, we'll finally meet the wonderful and super-common if
statement. Like foreach
, it's a language construct, and is one of those things that uses curly braces to surround a block of code. Where foreach
accepts an array and executes the code between its curly braces one time for each item, if accepts a Boolean value — in other words: true or false. If what you pass it is true, it executes the code between its curly braces. In this lesson we'll use the if
and else
statements, and see how to combine our conditions, along with a variety of operators, to get exactly what we need.
When you develop a website, it usually looks something like what you've been watching me do. You open up a PHP file in some sort of editor, then point your browser at some web address that executes the PHP file and shows you the output. Now, it's time to get your computer set up so that you can get programming, if you haven't already gotten set up and been following along. In this lesson we're going to explain how a web server delivers a page to a browser and then install a web server with PHP, using XAMPP. If you would prefer to use another web server package, we have a variety of operating system packages in our Installing a Local Development Environment guide.
Additional resources
More information on Development Environments
You've come a long way already, which has included using a bunch of existing, built-in functions. Now it's time to make our own! The first thing our PHP code does right now is read our pets.json file and decode it into an array, which we set on our $pets
variable. Let's invent a new function called get_pets()
that will do this work for us and return the finished array. Putting this into a function lets us reuse this logic whenever we want, in other places in our code, without having to write it all out again. This also makes organizing and updating our code more straight-forward and simple.
We want to update our contact page with the number of pets we currently have, which means we'll need to dynamically count our pets. We can use the get_pets()
function to get us that information, but we're going to have to learn how to load the function from one file for use on another file. We're going to create a new home for our functions, in a functions.php file, to keep them centralized, and then we will use the require
statement to use that file when we need it. We'll also discuss the other statement you can use in this instance—require_once
, include
, and include_once
—and why we're choosing require
over the others.
Additional resources
Tip: Why is not closing your PHP tags better when you don’t have to? Great question — see StackOverflow: Why would one omit the close tag?
To wrap things up on this project, we want to update the contact page with the same HTML layout as we have on the home page. To do this, we'll move the HTML sections into their own PHP files, and use require
again to get them into the right places.
Sharpen your object-oriented skills by exploring the ideas and reasons behind dependency injection. This simple principle separates developers who write functional code from those that are able to build great, and maintanable applications. In this series, we'll see dependency injection in action, why it's important, and how it relates to services and service-oriented architecture. We'll also refactor our demo application to use a dependency injection container, using a fantastic—but simple—container called Pimple.
In this tutorial, we’ll be coding with a real example where we create a simple app to help people give their money away, we’re calling it SendMoneyToStrangers.com. We’ve already bootstrapped a small app, which you can download and use if you want to follow along. It uses an SQLite database, so if you don't have that set up, you can grab the database and find installation instructions on the SQLite site. Once you SQLite, you will install the app from the demo code we are providing. We will also be using Composer to make it easier to get Pimple when we need it. If you are not familiar with Composer, you can watch the short tutorial The Wonderful World of Composer to get up and running.
Additional resources
SQLite site
The Wonderful World of Composer tutorial
Pimple
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
Our app is small now, but as it grows, the app.php file will get harder and harder to read. The best way to fix this is to separate each different chunk of functionality into different PHP classes and methods. Each of these classes is called a “service” and the whole idea is sometimes called Service-Oriented Architecture. In this tutorial you're going to create a new service, and learn about different ways to access external objects from a service. Then, we'll set up our app to use dependency injection to get the job done right.
Additional resources
Service-Oriented Architecture
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
We’ve already created our first service and used dependency injection, but one problem with the FriendHarvester is that we’ve hardcoded the SMTP configuration inside of it. What if we wanted to re-use this class with a different configuration? Or what if our beta and production setups use different SMTP servers? Right now, both are impossible! In this tutorial we're going to see how we can also inject configuration or an entire service. In the end we're going to inject the entire SmtpMailer object and build an interface for it. Through this process we'll make our code more flexible, since it will be able to accept any object that implements MailerInterface.
Additional resources
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
Our project now has services, an interface, and is fully using dependency injection. Nice work! One of the downsides of DI is that all the complexity of creating and configuring objects is now your job. This isn’t so bad since it all happens in one place and gives you so much control, but it is something we can improve! If you want to make this easier, the tool you need is called a dependency injection container. A lot of DI containers exist in PHP, but we're going to use Composer to grab the simplest one of all, called Pimple. (If you are unfamiliar with Composer, you should watch The Wonderful World of Composer tutorial.)
Additional resources
The Wonderful World of Composer
Composer download
Pimple
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.