In this next set of videos, we're going to
walk through various ideas around object-oriented
programming and how to technically implement
a number of different techniques that are
used within Drupal and also used across the
board on many object-oriented based projects.
So at its core, object-oriented programming
will give you a better model of how to organize
your code. Now, a lot of PHP programmers started
by simply hacking away and trying to get something
that's functional and what you end up with
is something called procedural code. You have
variables and you have functions, and the
role of the functions is to get from point
A to point B. It's to complete some kind of
task.
So for example, we might convert data from
one format to another or we might take some
data in an array and organize it according
to its alphabetical order. And the idea is
that you're constructing your code so that
you step through these different procedures.
Now, after you have some programming experience,
you start to realize that a lot of these functions
can be generalized so you can use them in
various other situations. So for example,
if you have that function that sorts items
in an array alphabetically, you might find
that useful to convert other types of data
into an alphabetically ordered list. For example,
some XML Or a set of strings.
And as you start to think this way, your mind
is actually going more towards object-oriented
programming. You're thinking about these functions
as these little machines that can be used
in different situations, not just the original
situation that they were intended for.
When you start to dive into tutorials about
object-oriented programming, you'll find that
people use real world objects as examples
because, in a big way, the world is very object-oriented.
The idea is that as you look around you, you
see yourself surrounded by these different
objects, and objects are basically anything
that you can give a noun to.
So for example, right now I'm surrounded by
my computer, a glass of water, and a lamp.
Because we can name each one of these things,
they are really objects. And in the real world,
objects aren't limited in the way that they
can interact with one another. So for example,
I can use the lamp to illuminate the keyboard
on my computer. I can use it to see through
the glass of water that I have. I can take
the glass of water and dump it on the computer.
And really, it keeps going but the idea is
that the interaction between the objects in
our world are limited more by our imagination
rather than any limitation in the object itself.
Now, if you apply that idea to your code,
then what you should end up with are these
components that can be used in ways that were
unanticipated at the start because you have
modeled the behavior of that item and contained
it in an object. And it turns out that this
is actually a pretty natural way for code
to be organized.
Now, the tricky part at first is taking that
idea and seeing how it relates to code. Because
most of the time when we're dealing with code,
we're dealing with kind of abstract concepts,
we're dealing with things that don't have
a parallel in the physical world.
For example, on a website, we have images,
we have forms, we have blocks of content across
the page, and templates. We can call each
of these things by their name which is a noun
and that indicates, at least on some level,
that they're objects. In addition, there are
objects that have more abstract ideas associated
with them. For example, a calculator that
performs some operation on the data that gets
sent to it or, in the example of our function
that sorts alphabetically, we have a sorter.
Those little bits of code are like machines
and they're our inventions and we can give
them whatever name we want, but they are objects
that didn't really exist before we created
them. Now, if you don't immediately see the
application of this different way of looking
at your code, that is, looking at it as objects
rather than a set of procedures, that's okay.
That sort of perspective comes from actually
working with it and seeing how the code is
organized and what kind of benefits arise
from organizing it in that different way.
So just let some of those ideas start to percolate
and we're going to start getting our hands
dirty. And along the way, we'll talk about
the implications of thinking of your code
along these lines.
