This Cyber Monday Tuts+ courses will be reduced to just $3 (usually $15). Don't miss out.
For those who are just starting this series, note that we are taking a look at object-oriented programming using PHP within the context of WordPress.
The Target Audience
We're doing so from the perspective of the very beginner, so if you're an experienced developer, or are familiar with many of the aspects of object-oriented programming, then this series is probably not for you.
With that said, if you are interested in learning object-oriented programming in PHP and you do consider yourself a beginner, then we're going to continue the series in this article by talking about some of the data types that PHP offers.
But first, make sure that you've caught up on the previous articles in the series as they each build on one another:
Once you're all caught up, we can begin talking about types.
What Are Types?
We ended the last article with the following:
We'll get to that, but first we need to discuss some of the more primitive aspects of programming such as strings, arrays, loops, and conditionals.
Now, strings, arrays, and so on are what we consider types. Loops and conditionals are what are known as control structures which we'll be covering in the next article.
So in the meantime, let's come up with a simple term for what a data type represents so that we know how to conceptually model them moving forward not only with this article, but with the rest of the content in the series.
For anyone who has written any code that includes variables, then you've likely seemed something like the following:
$name = 'Tuts+ WordPress';
$is_active = true;
$age = 42;
- ...and so on.
In the most basic form, you'll likely hear the above code defined as variables with definitions, and that's correct, but it's a very generalized definition.
You see, whenever you define a variable in PHP, you so by prefixing the variable with
$. This lets PHP know that you're defining a variable, and that it will represent a typical value.
Easy enough, right?
The thing is, there are different types in PHP. For those who are coming from what are known as "strongly-typed languages," we'll talk about that more momentarily, but know that PHP offers the following data types:
- floating point numbers
- ...and more
Everything from booleans through strings can be thought of as a simple data type where as arrays and objects can be considered more complex data types.
Simple Data Types
Simple data types are defined as such because the data that they represent is, y'know, simple. That is to say that it will normally fall under the banner of true, false, decimals, and words and/or sentences.
To give concrete examples of each of the above:
- Boolean values are intended to hold the value of
- Integers refer to any whole numbers. That is to say that it includes no fractional or decimal components to it. This includes numbers such as
5000, and so on.
- Floating Point Numbers are kind of the opposite of Integers in that they do represent values with fractional parts. So, for example, you may see
100.25, and so on. If you were to try to save the value of, say,
3/4then you'd actually save
.75unless you saved it as a string. That is, you were to save
'3/4'. Notice the subtle delineation between the two? One has quotes and one does not.
- Speaking of strings, strings represent any single word or set of alphanumeric characters that make up a series of letters and numbers. This may be a single word, it may be sentence, it may be sentences, and it may be a random series of characters such as an encrypted string. Examples of strings include
'phrases like this'or a single
'word'or even something more complex like
Here's the catch, though—any of the above data types can be converted into strings by wrapping them in quotation marks.
But There's a Catch
For example, if you were to work with the boolean value of, say, true but you were to store it in a variable like this:
$is_active = 'true'; then you've actually just created a string that reads
Similarly, if you were to write a string that read
$age = '42'; then you've created a string containing the number
42, not an integer.
The reason that this matters is because when it comes time to working with control structures—which we'll review in the next article—is that sometimes, running comparison can have unintended consequences if you're not sure which data type with which you're working.
Complex Data Types
In addition to simple data types, we also have complex data types which, in my opinion, aren't really that complicated. In fact, I think of it as a fancy way of saying that we have ways of storing multiple pieces of information into a single variable; whereas, with simple types, we have a way to store single pieces of information in a single variable.
Pretty easy to remember, right? So what are some of the more complex data types?
The two primary complex datatypes that we're going to focus on in this series as arrays and objects. There are more, but they are outside the scope of this series, so if you're interested, then feel free to take a look at the PHP manual, but I warn you: if you're an absolute beginner, the content may feel a little overwhelming.
- Arrays are ways of storing multiple pieces of information into a collection. Consider, for a moment, that you have a set of names that you want to store into a collection aptly called $names. In PHP, you'd define an array and assign it to a variable like this:
$names = [ 'John', 'Paul', 'Ringo', 'George' ];Notice here that we have an array of strings, but another aspect of PHP arrays is that they can hold multiple data types. That means you can have an array that looks like
$various_types = [ 42, TRUE, 'Star Wars', .007 ];. Kinda cool, right?
- Associative Arrays are just like standard arrays that we see about except that they are made of key value pairs. This means that each value is retrieved by a key, so if we were to convert the first array into an associative array, then it may look something like this: array( 'rhythm-guitar' => 'John', 'bass' => 'Paul', 'lead-guitarist' => 'George', 'drums' => 'Ringo' ); If the format looks like weird right now, don't worry about it! We'll be taking a look at these in much more detail later in the series. The primary take away is that standard arrays are index numerically - that is,
3, and so on - and associated arrays are indexed by their keys such as
'drums', and so on.
- Objects, which we touched on last time, are arguably the most complex of the data types offered by PHP because they are a combination of everything. Their attributes are made up of data types or even other objects, they are functions, and they perform operations not only on other simple data types, but on complex data types as well.
As far as objects as concerned, the best way to see just how complex they can get is to see them in action, which we will be sure to do when we build building a plugin.
A Note About Arrays
Note that in some languages, the size of the array must be declared before you can actually use it. For example, in C, you have to tell the compiler that the array will hold exactly, say, 10 values.
In the case of PHP, this is not the case. PHP's array are dynamic meaning that you can continue to add data to the array without needing to increase its size. Yes, as with anything, there are limits, but it's usually related to the size of the data being stored, or the amount of memory that a machine has available.
Don't Let the Phrases Intimidate You!
But for the time being, don't let the words simple and complex intimidate you. Granted, "simple" is relatively straightforward, right? But "complex" has this connotation that it's going to be hard to understand, and that's not true - hopefully this article is demonstrated that.
Complex data types can simple hold multiple pieces of data. Simple data types cannot.
Are There More?
Yes, there are. Namely resources, callbacks, and pseudo-types. Most of these are out of the scope for this particular series; however, we may end up using callbacks (which are a certain type of functions) later when we begin building our plugin.
Feel free to read up on these as much as possible, but know that they are beyond the scope of the content that we'll be covering through this particular series, so if you feel overwhelmed by them, perhaps revisit them once the series is complete.
A Word About Strongly-Typed Languages
Some programming languages are known what is dynamically-typed (or weakly-typed) where as others are known as strongly-typed languages.
This is another case in which the terminology often intimidates new programmers rather than empowering them to know how to use the tools that are available to be, but the distinguishing factorings among the two is really quite simple:
- Strongly-typed languages means that the variables must first be declared as a certain type and they can only hold a certain type of data. For example, if I wanted to hold a string, then I would declare the variable as
string sMyName; Then, I would store a string into the variable.
sMyName = 'Tom McFarlin';. This means that the
sMyNamevariable can only hold strings. If you try to store another data type in that variable, then the compiler will throw an error.
- Dynamically-typed languages, such as PHP, allow a single variable to hold multiple types at any given point of execution. In using the example above, I could use as easily say
$my_name = 'Tom McFarlin';as I could
$my_name = false. If you try to store another data type in that variable, then it works fine. You can see that this has both advantages as disadvantages.
Where To Next?
Before we return to the world of object-oriented programming, we need to talk about a a few additional basic structures of programming such as control structures.
These include things such as loops, conditionals, and so on that can impact the flow of code throughout the lifetime of the program. Additionally, they work hand in hand with the data types that we've outlined here.
So before we head into the next article, make sure that you review everything that's going on here, leave comments, questions, and feedback in the comment feed.
I'll see you in the next article!