In our PHP programs so far, we’ve relied on data types built into the language—we’ve used String, Number and Boolean data types directly and saved data in variables. We’ve also used Arrays to organize multiple pieces of data within a single structure.
But sometimes the data types built into the language aren’t enough! Just as we can write custom functions and not depend exclusively on PHP’s built-in functions, we can write custom data types.
In order to define our own data types, we’ll think of the general qualities our user-defined types should have. We’ll create a class —a blueprint defining the related data and functions that should be bundled together inside each instance of this new type. Once the class is defined, we can create specific instances of it—as many as we want! These instances of the class are called objects.
Imagine we were creating a
Pet class within our program. What sorts of data and functionality should every pet have? Spend some time thinking about this yourself and then feel free to scroll down and look at some of our ideas.
v | | | | | | | | | | | | | | v
We imagined that pets should have data like their name, color, age. We also thought they might have some functionality like eating and cuddling.