In this tutorial, I will show you how we can build a better blueprint and hierarchical structure for our app’s classes through the use of abstract classes.
Additional resources
In this tutorial, I will show you the power of abstract classes and how you can enforce subclasses to define certain functions. In this way, you can share logic but for things that are really specific to subclasses, you can force them to define certain functions, but with the specific logic required.
Additional resources
In this tutorial, I will show you how to extend an abstract class and ensure that all the methods that are required by the abstract class are included in this new class.
Additional resources
In this tutorial, I will show you how to handle new requirements for data storage by creating two smaller abstract classes out of our original data storage class. By creating these new abstract classes, we can build more flexibility into our code.
Additional resources
Now that we have created new abstract classes, we need to load and utilize them in our code. In this tutorial, I will show you how we can refactor our code to make use of our new abstract data loading classes.
We have an abstract class with methods that we require, but there’s no logic in these abstract methods. This is a perfect opportunity to use interfaces. In this tutorial, I will show you how to create and implement Interfaces. I will also explain how interfaces are useful when you will be sharing your code. But even if you’re not creating interfaces to share, chances are, if you’re using open source code — and definitely in Drupal — you will need to know how to implement interfaces.
Additional resources
Managing Media in Drupal
CourseConfiguration Management
CourseLet'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?