PHP27.0 Object Oriented Programming Basics and Access Control

Unlike some other programming languages, PHP
itself is not actually object-oriented based. PHP is perfectly fine with us just going on
and writing all of our PHP code in a procedural style as we’ve been doing up till this point.
However, object-oriented programming does have some advantages to offer and because
of that, in more recent versions of PHP object-oriented programming has become an option. PHP offers
us different tools and language constructs that we need to be able to do object-oriented
programming if we choose to. What kinds of advantages do we get by writing our code in
an object-oriented style? Well, the object-oriented way of writing our programs was basically
created because it offers improved reusability of the code we write making it easier for
us to both take the code that we have written and use it multiple times in multiple places,
potentially in multiple projects. It also allows us to better reuse the code by taking
code that we’ve already written and being able to more easily extend and modify that
code so that we can potentially put it to work in situations that it perhaps wasn’t
originally intended for. What I’d like to do here in this file to demonstrate some of
PHP’s object-oriented capabilities is I’d like to say that we’re writing ourselves
a little program here and this program is going to have a lot to do with boxes. Make
up whatever excuse you want, but basically the idea here is that boxes are going to be
something that’s going to be crucially important to us here in this program. In order to be
able to create what would be referred to as box objects, what we would first need to do
is we would need to create what’s called a box class. I’m going to insert a new set
of PHP tags up at the top of my document and in that section up at the top I’m going
to create a new class called box. Whenever you want to create objects in PHP, those objects
have to be created from a thing called a class and this is the basic way that a class in
PHP looks. It starts off with the keyword class followed by a name that you give it.
The name is essentially an identifier; it’s something that you choose. It is traditional
to capitalize the name of your classes although it’s not actually required by the language.
When we actually have a class like this, from this class we’ll be able to create box objects
when our script runs. What is an object actually? Well, an object is actually a combination
of pieces of data and actions that can take place, typically on those pieces of data.
So in procedural terms what we would be talking about is an object being a combination of
variables and functions. Here in my box class if I decided that the two most important aspects
of a box that I needed to keep track of were the box’s width and height I could do that
by declaring variables to hold width and height in my box class. I’ll have a width variable
and a height variable. Those two variables though when I declare them, I need to declare
them in a very specific way. Because of the rules of scope that PHP has which we’ve
discussed previously, if I were to simply declare these two variables here inside this
block making up the body of the class, those two variables would only be in scope basically
inside those curly braces and in this particular situation those variables actually need to
become a part of every object that’s created from this class. So really we need for these
two variables to not have function scope. We need them to have what is called class
scope; we need to be able to access them from inside the class. There’s going to be several
different ways we’re going to see that we’ll be able to declare variables with class scope,
but one of the first and simplest is to put the keyword var in front of those variables.
When we declare a variable inside a class with the keyword var in front of it we’re
essentially saying that variable will be a variable that will be accessible to an object
created from this class. So if I create a box object from this class, that box object
will have a width and height variable that will be accessible. If I create 1000 boxes
from this box class, each one of those thousand boxes will have its own width and height variable
that will be accessible. So width and height then become two, what we might refer to as,
data members of every box object that’s created, essentially a place that each box
object will be able to store a little bit of data about itself, in this case its width
and its height. I mentioned that besides storing data, objects usually also store actions or
behaviors or what we would call functions that can actually work on that data. So let’s
say that I wanted to create a function for my box class here and I want the function
to be called show_box. I could go ahead and create show_box like I would any other function
and for right now I’m just going to put an echo statement in it. I’m just going
to have the echo statement say I am a box. With just this little bit, we essentially
have the basic parts we need for a pretty fully-formed class. We have our box class
so we can create box objects. Every box object we create then will have a width and a height
and every box object will have a show_box behavior which will print out I am a box.
So how would we then actually make use of that class? Jumping down here to the body
of my document, I’m going to put in another set of PHP tags and in this set of PHP tags
let me actually work with those box objects. Whenever I want to create an object from a
class that I’ve written, I do that by using the keyword new. The keyword new is a command
that’s being given to the language that instructs it that we want to have a new object
created. Whenever we use the keyword new we also then need to tell it which class to create
the object from. So here I’m going to say create a new object from my box class. The
name box here matches the name of the class that I had written up above. The parentheses
after it we’ll get to here in a little while. When this new statement actually executes
a new box object will be created and will be returned. I can then take that box object
and store it in a variable. So I’m going to create a new variable called B1. I’ll
put the new box that’s created there into my variable B1 so that I’ll then be able
to reference it, still be able to work with essentially. So what’s actually going on
here? When I actually tell PHP that I want to create a new box, PHP performs a process
called instantiation. It’s a little bit in a way like declaring a variable. PHP will
go out and claim some of the computer’s memory in which a new object can be stored,
but at that point the object is pretty empty, it’s pretty blank, it’s just an empty
bit of space. PHP then though will look at the class that is being used to create that
object and it will use the class as something like a blueprint or a prototype where it will
see what variables are declared in the class and whatever variables are declared it will
create inside that new object space. So that object that’s created out in memory will
get a width variable and will get a height variable. PHP will then also take any functions
that are declared in that class and will make those available to the object as well. So
my new box object that I’m creating, B1, will have its own width and height variable
and will have access to the show_box function. All of that then is wrapped up here in a nice
little package here inside my variable B1. Well, what if I would like to actually do
something with it then? What if I tried saying echo B1? What I would be doing at that point
is I would be asking PHP to print out B1 as if it were a string and if I take a look at
that in my browser I get an error message saying Object of class box could not be converted
to a string. So PHP definitely does recognize that my variable B1 has a box object in it,
but when I try and tell it to convert it into a string it does not yet know how. That’s
something that we’ll actually have to teach it. I did give my box class this show_box
function. The show_box function echoes out some information. Maybe instead of echoing
out B1 itself, maybe what I could do instead is I could use the echo that’s inside the
object B1. In B1 there’s a function called show_box. How do I actually access that function?
Well, whenever you want to access something that’s inside an object you do it using
PHP’s arrow operator. The arrow operator in PHP is a ->so it actually looks like an
arrow and what it actually means is to access the thing that’s inside the object. So here
I’m saying run the show_box function that’s inside the object B1 and when I take a look
at that in my browser that works just fine. It actually does print out I am a box just
like the show_box function said that it should. Let me come back up to that function. I’m
going to throw a break on the end of it. If I call that function a second time, I of course
now get I am a box 2 times. So I can, just like any other function, call functions that
are inside or that are a part of an object as many times as I happen to need to call
them for whatever reason I happen to need to call them. If I can use the arrow operator
to be able to access functions that are stored inside an object, could I also then use the
arrow operator to be able to access variables that are stored inside an object? Could I,
for example, say echo The width of box 1 is, and then I’ll append in B1 arrow width followed
by a break. So B1 arrow width, meaning the width variable that’s stored inside the
object called B1, would that work? Again, let’s look in the browser. No error was
produced. I do see the line The width of box 1 is. I missed the word of in there; I’ll
have to fix that. And then I’m actually coming up with a blank. The blank, of course,
is because we’ve never initialized the width of box B1. Could we do that with the arrow
operator as well? If I say B1 arrow width equals 200, assign a value of 200 to the width
variable that’s inside of object B1, that seems to work okay. On that next line then
when I try to access the value of the width I’m now coming back with the value that
I assigned. So width is acting just like a normal variable, I’m just having to access
it a little bit differently because instead of that variable just sort of being out in
the open, that variable is enclosed, it’s encapsulated in a way inside this new object.
So I put a value into the width variable that’s inside object B1. I can then access the value
of the width variable that’s inside the object B1. What if I had a second box? If
I were to duplicate my box one instantiation line and create a new box called B2, if I
were to then assign a value to the width of B2 – I’ll make B2’s width 100 – is that
going to have any effect on the width of box B1? Nope, B1 is still coming back as 200.
The width and height variables, because they’re declared with var, means that each individual
object created from this class will have their own version, their own copy if you will, of
those variables so when I create object B1, object B1 has its own width and height. When
I create object B2, B2 has its own width and height. The width of B1 and the width of B2
are two separate variables. Yes, they have the same name, but they are stored inside
two separate objects. The same thing’s then true with the height. The two functions do,
however, to actually share the show_box function. The show_box function itself is not truly
stored inside either object B1 or B2. It’s instead simply made accessible to both of
those two variables–to both of those two objects more appropriately. Because the show_box
function is shared between the two, how could we then handle a situation where inside that
function we might want to access data about that individual object? This show_box function,
for example, instead of having it print out generically I am a box, what if I actually
wanted it to print out the width and height of the specific box that the function was
called for? Well, here’s what I’d like to do. I’d like to have show_box change
so that it actually does print out the width and height. Let me modify this function. What
I’m going to do is instead of having it echo, I’m going to have it return a string
and in that string I want to have the box’s width. Appended to the width I’m just going
to have the letter X. So it’ll be width, x, height, kind of like 4×4, I suppose. And
then after the x I’ll append in the height and then I’ll put a break at the end just
to make it cleaner if we run it multiple times. So here when I’m writing the class itself
it almost seems obvious that when I talk about width or height that I would be talking about
width and height up here, same variables, right? Well, if I actually now come back to
my show_box function and for B1 I try to echo what’s returned when I call show_box, what
we’ll find in the browser is a bunch of errors. I ended up getting errors for trying
to access the width and the height. Both of those errors are occurring on line 8 of the
script. If we take a look at line 8, the line it’s actually complaining about is the line
inside the show_box function. So even though it appears to be a simple thing that width
and height here in the show_box function would be referring to the width and height that
are referred to here in the class, it’s apparently not quite that simple. For starters,
if you think about it, there would be scope issues trying to access the width and height
this way. Variables that are declared outside a function in PHP are typically not available
automatically inside a function, so there’s already a little warning flag that should
be going up right there. The other question is, when I call show_box for box B1, how does
it know the width and height that I’m talking about here are the width and height for B1,
not the width and height for B2 or some other width and height altogether? The way that
we resolve all of these problems is with another new keyword. Actually, it’s less a keyword
than it is just a very special variable called this. The this variable, besides being a variable
that’s difficult to talk about because it’s name is this, what it is is it’s a variable
that always refers to the current object. If I put the this variable in in my show_box
function where I access both the width and height, essentially what it means is that
whenever I talk about the width here, the width that I’m talking about is the width
for this object, this object meaning the object that show_box was called for. So if the show_box
function is called for box B1, box B1 is this, so it’s box B1this’s width that I want to
use here in my output. Again, if show_box is called for box B2, then box B2 is this
so when I talk about the width and height of this, I’m talking about the width and
height for box B2. Down below in the body when I’m actually calling show_box down
here, I’m doing it both times for box B1. Let me go ahead and change that to box B2.
Let me bounce back up and let me also add in a setting for the height for both of my
two boxes. For the height of B1 I’m going to set its height to 100. For the height of
B2 I’m going to set its height to 200. So B1 is a 200 x 100 box; B2 is a 100 x 200 box.
If I go over and take a look in the browser now the errors all go away and both of the
two boxes are properly printing out their width and height. They’re able to do that
because they know which width and height to use because in the show_box function we’re
now using the this variable. So when I call show_box for B1, it’s B1’s width and height
that are used. When I call show_box for B2, it of course is B2’s values that end up
being used. So what we have so far is we have the ability to create classes. When we create
classes we can declare as many or as few variables as are necessary to store data about the objects
that will be created from that class. We also have the ability to create functions. With
functions, we’re basically tying actions to the data that is stored inside a particular
type of object. When we want to access the variables that are available inside an object
from one of the object’s functions, we need to make sure that we access those variables
by doing $this and then the arrow operator. The arrow operator itself is a new operator
in PHP used specifically with object-oriented programming. It means the thing inside the
other thing, really. So the width that’s inside of this object is what we would be
talking about there. And we can use that arrow operator to access variables that are inside
instantiated objects, we can use it to run functions that are inside objects, and any
time we want to create a new object we can certainly do so by using the new operator.
Let’s add another function here to our box class for the fun of it. What I’d like to
do with this next function is I’d like to provide the ability for a box to actually
draw itself on the screen. By draw itself, essentially what I mean is that I would like
for a box to essentially be a div. The div will need to have a class of box. And then
what I’ll be able to do with it is I’ll be able to do some in-line CSS where I’ll
be able to set a width and a height for that particular div and I have some CSS in my browser
set up so that when I do that it will simply draw that div with an outline, with a border
around it, so that we can see our box that we’re creating. So I’d like for there
to be a function in my box class that will basically write this HTML code for me. Let
me go ahead and cut that code out. I’m going to take it up to my class and in my class
let me add a new function. I’m going to call the function draw. I’m going to have
that function then return essentially that same HTML that I just wrote, except at the
point where previously I hardcoded in the value for the width, what I would like to
do instead right there is append in the width for the actual box whose draw function has
been called. So I’ll say for this box right here access its width. Same idea over here
for the actual value of the height of the box. I will append in for this box its height.
So at that point I should have the ability to be able to call the draw function for my
boxes which will output this bit of HTML which combined together with the CSS there at the
bottom of my screen should result in an actual box showing up on my page. Let’s see how
that works. I’m going to jump down here to the body and after I’ve called show_box
for both of my two boxes let me try calling the draw function for both of my two boxes.
Taking a look at that in the browser that’s the way it actually appears. So the first
box 200 x 100, wide and short; the second box 100 x 200, narrow and tall. It looks like
it’s doing a fine job drawing my boxes. Is all this object-oriented work really worth
it? Obviously, there is an extra level of complication that we’re getting into here
by creating these classes. If you wanted to think about it, you could certainly probably
imagine a way that all of this same capability could be done without ever venturing into
object-oriented programming at all. If I had boxes, why couldn’t I just have width and
height as regular variables out in my PHP script? Why couldn’t show_box and draw just
be regular functions out in my PHP script? Well, by all means they certainly could be.
This is essentially providing us with a different technique, a different style, that we’re
able to do the same work in that some people feel has nice advantages. Certainly one of
the things I like about this particular solution, representing boxes with objects rather than
standalone variables and functions, is what would happen if the number of boxes I had
increased? Right now I have two boxes. Because of my object-oriented style, that means that
essentially I have two variables; I have two B variables – $B1 and $B2 – and those are
the only two top-level variables I actually have or have to deal with. If I had 1000 boxes
to deal with, then I would have 1000 variables or maybe I would just create one array and
in that array I would store all my box objects. If I were to do this same thing though without
object-oriented programming and I had 1000 boxes, then I would need 1000 widths and 1000
heights. Or, at the least, probably two arrays – one for all the widths, one for all the
heights. Also what about the functions? The way the functions work right now, show_box
and draw, the way they work in this object-oriented style is I call them directly from an object.
I can say, Hey B1, show yourself, show a box. I can say, Hey B2, draw yourself. Notice that
neither situation am I actually having to pass anything to those functions. Because
of the functions being a part of the object itself–er, accessible directly from the object,
they already have access to the data that they need to be able to run without me having
to pass anything to them. If those same two functions were written outside of an object-oriented
setting like we have here they could certainly still be made to work, but how would the show_box
function know what box to show? Well, it would probably need to be passed a couple of arrays,
or at a very minimum, would need to be passed a width and a height. So the show_box function
would get a little bit more complicated in what it would have to do every time I called
it. The same exact thing would also be true for the draw function. The draw function would
have to be passed information so that it knew which box to draw whereas here in this object-oriented
way of doing things it knows because of the object that the function was actually called
for. I mentioned previously that one of the advantages of writing things in this object-oriented
style was the ability to be able to more easily reuse our code. One of the ways that we’re
going to learn to be able to more easily reuse our classes that we write like this is through
a process called inheritance that we’ll talk about later. The other way that we can
more easily reuse our code though, something that we can start thinking about right now,
is simply by being able to store this information in a file and take that file from project
to project. Let’s say, for example, that I wanted to take my box class and I wanted
to make it more reusable than it is right now. Well, one way I could certainly do that
is what if I took my box class and I simply moved it over to a separate file, call it
box.php, for example? So here I’ve taken my box class, I’ve moved it to a file called
box.php. Over in the file I was working in before then, in order to still have access
to my box class I could simply require the file that contains my class. So I’ll just
require box.php and in the browser things continue working exactly the same way they
did before. So with my box class off in a separate file now, what could potentially
happen is I could fairly easily pick that file up and move it from project to project,
or store the file in one central location where all of my projects had access to it.
That would be a nice easy way to do it. But what happens as we take this box class from
project to project as sort of the scope, sort of the context, sort of the domain we’re
working in changes? What I refer to is here in this particular example. Hopefully it made
a bit of sense what we were talking about when we referred to the width and the height
of a box. Here I was being pretty particular, I was working down in the HTML and CSS levels
to actually draw the box, but what if the box at some point was used in some other context
where maybe we weren’t necessarily thinking as specifically about the code as we are here?
Is there anything in my box class that could potentially help protect boxes from accidentally
being misused if they were put into some context where maybe having strict numbers for width
and height did not make as much sense? What I refer to is–Right now, sure, 200 for a
width made perfect sense, but what if in some other way at some point me or somebody else
using my box class decided that it would make sense to set the width of a box to very wide,
let’s say. The way my box class actually works right now where I’m actually taking
values and putting them directly into the variables themselves, there’s nothing standing
in the way of keeping something like this from happening. If I go over and take a look
in the browser now that first box of mine collapses down to essentially nothing because
the actual size of that box is now showing up as very wide in those–in the CSS. Since
the CSS doesn’t understand what very wide means, it’s essentially collapsing it down
to 0 pixels. It might seem almost silly thinking that something like this could happen, but
as this class continues to be reused and reused and reused by myself, by other people in the
future, and in lots of different projects and in lots of different ways, there is an
ever-increasing chance that at some point somebody is going to misunderstand exactly
what the width of a box is supposed to be and they’re going to put some value into
it that is not appropriate, is not something that the class was prepared for. So is there
a way that we could write our class where the objects of that class could potentially
protect themselves from this type of purposeful or inadvertent misuse? Yes, there definitely
is something we could do. It’s called access control. In our box class when we actually
declared our width and height variables we declared them by using this keyword var which
basically just said these are variables that will become a part of every object created
from this class. In an access control point of view, declaring these variables with var
would be exactly the same as if we declared them using the word public. The word public
is another word that we can actually put there on those variables. The word public does what
var did in saying Yes, these variables will become a part of every object created from
this class, but the word public also goes a step further in that it explicitly says
anybody from anywhere, basically anybody from inside this class or any code from outside
this class, should be able to change the value of these two variables. This public access
modifier can be put both on variables and on functions so if I wanted to I could come
down and I could declare my functions also as being public. Functions and variables are
both public by default so there’s really not a whole lot of reason that I ever have
to put public in front of any of them, although it can sometimes be good for sort of stating
your intentions or making your intention obvious that you literally wanted something to be
made public and the fact that it’s public wasn’t something that occurred accidentally
as a result of an oversight. With that word public in there if I jump back over to the
browser now you can see absolutely nothing has changed. So public was the default, now
I’ve declared public explicitly; nothing has changed at all. Where things can start
to get a little bit different is that I have another option besides public in that I can
also declare things as being private. When I declare something as being private, this
is not the default so this actually is going to cause a change. When I declare something
as being private, essentially what I’m doing is I’m limiting who or where that particular
thing can be accessed from. Here when I’m declaring my width and height variables as
being private, what I’m saying is that these two variables can only be accessed from within
this class, from within the object itself if you want to think about it from that point
of view. So when I’m trying to access the width and height here in these two functions
these will be allowed, these are fine because this is code that’s inside the class, that’s
from inside the object itself so my show_box and my draw function will still work perfectly
well even when I’ve declared these two variables as being private. The code that I have over
in my viewable PHP file, however, where I’m changing the values of the width on these
two lines and the height on these two lines and then access the width on this line, those
lines now become illegal because this code is not a part of the class, not a part of
the object itself. This is code that is executing externally from the class itself and that
is what is stopped from happening when things are declared as being private. If I jump over
to my browser now, hit refresh, you can see all of a sudden I get a fatal error. The fatal
error is coming from line 30 where it’s saying we cannot access private property box::$width.
That means the width variable that’s inside the box. The double colon (::) there is the
scope resolution operator, similar to the arrow operator but slightly different in how
we use it and we’ll talk about it later. But this error is letting us know that the
width variable inside a box is a private property and cannot be accessed because of it being
private. So it’s line 30 where that error is showing up. Line 30 right here is the very
first time that I tried to access the width. If I were to comment that line out, the error
just moves down to the next line, line 31. It’s not until I get rid of all of these
different lines where I’m trying to access the width and height from outside the class
itself that I can actually get those errors go away, and at that point I’ve then lost
all my actual data. I have nothing by nothing for my two boxes and for the boxes themselves
they’re showing up 0x0 pixels because they have no width or height at that point. So
by declaring the width and height, those two variables, private, we’ve prevented anybody
but the class itself from being able to access those variables. We can still use the variables
from inside the class itself like we are in our show_box and draw functions, but the variables
can’t be accessed by any external entities from the class itself. So in a way you could
say that that solves the problem that we were trying to solve. We wanted to be able to prevent
somebody from putting a value in, purposely or otherwise, that was inappropriate for the
variable. So we wanted to stop somebody, for example, from being able to set the width
of a box to very wide and we’ve now done that. Now we can not execute this line where
we set the width of our box B1 to very wide, but we’ve in the process now created a whole
new problem in that we now have no way to actually put a width or height into our variables.
We can solve that problem, though, by kind of following the lead of our show_box and
draw function. Since our show_box and draw functions are part of the class itself, they
still have access to those private variables, those private data members, width and height.
So if those two functions can still access those variables for their own use, could we
not then write public functions that could access those variables for the purpose of
actually initializing them–er, actually changing their values? Here’s what I’m thinking.
What if we created a new public function? We could call it set_width and I could say
that function is going to be passed a value – I’ll just call it W. In that function
then what I could say is For this box, the box that this function is being called for,
set it’s width equal to the value of whatever is in the W variable. So I could call this
function for one of my boxes, pass it what I want the width to be, and this function
would then take that width and put it into the width variable for me. Back over in my
displayable file that would mean that on a line like this where I was setting the width
for B1 I could now change that. It could become B1 call the set_width function and pass it
a size. I’ll set it to 200, for example. If I jump over to my browser and take a look
that seems to have worked, the 200 for the width of box B1 went in and is being used
for that box properly. I could do something very much like that then also for B2. When
I go to set the width for B2, I’ll set it’s width to 100. That seems to be working. And
since that new function of ours worked so well for the width, couldn’t we do the same
thing then for the height? I can make a new public function; I’ll call it set_height.
I’ll say this function is going to be passed a variable called H and when this function
runs for this object’s height, I want to assign it the value that’s in the H variable.
There we are. Let me give it a try. For box B1 I’m going to set its height to 200, for
box B2 I’m going to set its height to 100. Back over in the browser, refresh, and there
we are. We’re back where we were. So despite the fact that I took those two variables and
made them private so not anybody can access them, I’ve now restored my ability to be
able to put values into those variables by using these two functions, these two functions
I’ve made up called set_width and set_height. If I wanted to be able to have the ability
to access those two variables independently I could certainly create functions called
get_width and get_height. They could simply return whatever value was in the width or
height so that I would be able to get at those two values independently. If I wanted to be
able to, for example, put this line back in where I’m saying the width of box one is,
since I can’t say B1 arrow width because that would be accessing a private variable
directly, what if I changed that to B1.get_width and turned get_width into a function? Back
over in my class I could put in public function get_width. I don’t really need to pass it
anything. Instead, I could just have the function return the box that it was called for’s
width. While I’m at it for good measure what if I go ahead and also make a get_height
function. The get_height function can be called get_height and it will simply return this
object’s height value. In the browser, now I’ve got my line up at the top that says
The width of box 1 is 200 back. In object-oriented terms very often we find ourselves needing
to create these kinds of functions. What we’ve essentially done by using private up here
on our variables is we’ve provided our class with what’s usually referred to as encapsulation.
Encapsulation essentially just means that we’ve built something like a wall around
objects of our class so that the object is responsible for its own data. Code from outside
the class can ask the objects of the class to store or return a piece of data, but through
these functions it’s then ultimately up to the object itself to decide whether they
actually want to do it or not. Since these are functions we could build extra checks
into these if statements where we could decide do we really want to accept this width, do
we really want to accept this height. Through this encapsulation, essentially our objects
become protected from the outside world which is another layer in the solution to making
this class more portable in that if I take it and I pick it up and I move it to a different
project or use it in a different way, because the class maintains a wall between itself
and the outside world there’s less chance that as I move it from project to project
something that’s happening externally could lead to a corruption inside the object itself.
As far as the set_width, set_height, get_width, get_height functions go, they themselves are
sometimes referred to as setters and getters. Sometimes people refer to the functions that
are used to set values in an encapsulated object as setters. Others sometimes will call
the functions used to get values getters. Other terms you may hear is sometimes the
set functions are referred to as mutators, functions that mutate or change an object.
The get functions are sometimes referred to as accessors, functions that let us access
different values inside the class or inside the objects. Have we achieved good encapsulation
here? If I go back over to my displayable page again, remember what started this whole
problem was being able to set very wide for the width of my box. Can I not still do that?
I sure can. I can still right now put in very wide for the width of my box. So all that
trouble and in a way I actually haven’t really fixed the problem yet, but could I
at this point, I think maybe is a more important question. The way things were before was we
didn’t have any immediate way to even be able to stop somebody from being able to assign
an invalid value to the width, but now with this new encapsulated setup, by forcing values
that are going to be put into my box objects to go through functions, I now have a very
easy and logical place where I can determine if a value is appropriate before I allow it
to be set. So for here in this case I’m talking about my set_width function. What
I’d want to do is I’d want to make sure that any width being set, any value of $W,
I would want to make sure that that value is numeric and I would want to make sure that
it is positive, greater than zero. I would be able to do that at this point pretty easily
just with an if statement. I could use PHP’s is_numeric function. I could say if $W is_numeric
and if $W is, let’s say, greater than or equal to zero, if both of those things are
true then I could go ahead and assign the value of $W to the width. So set_width at
that point then is protecting the width variable; set_width is something like a bouncer or a
gatekeeper for any incoming width data. Any incoming width data can only get into my class
now through the set_width function because the width variable itself is encapsulated,
it’s private, and whenever a new piece of data does come in through the set_width function,
the set_width function now has the ability to evaluate that data and make sure it appears
to be appropriate before it actually lets it stick around. If the incoming piece of
data doesn’t appear to be appropriate then it won’t get assigned to the width variable.
If we wanted to we could just leave it at that and just say No value is assigned or
if we wanted to we could go ahead and provide something of a default value. I think that’s
what I’ll do here just so we can see it a little bit better when it happens. I’ll
just say essentially if we try to set a width and that width is invalid then we’ll use
a default width of 50. If I jump back to my browser now, there we are. You can see I tried
to set an invalid width of very wide for my box 1, but because that value was determined
to be invalid we went with our default value of 50 instead. If instead of very wide if
I try to put in an actual proper value like 200, then that was happily accepted by the
set_width function and used. So that went in there with no problem whatsoever. So at
this point our box class, while maybe not the most terribly useful class in the entire
world, is a–is a working little example. It is a good example, I think, of an encapsulated
class, a class capable of generating these encapsulated objects. That makes it more reusable.
I think I’ve pointed out some of the advantages that writing it this way has. When we start
talking about the encapsulation, the use of private and public and such, obviously that
goes well beyond what we would ever be able to do if we were just dealing with standalone
variables and functions, so another advantage in object-oriented’s favor. The only thing
I can think of that I would have left to finish this off would be to maybe duplicate this
data validation that I’m doing in the set_width function and do it for set_height as well.
It’s really the exact same thing that I had before in set_width, just working specifically
for the height instead. Other than that the idea is the same. We still have a lot more
about object-oriented programming to talk about, but these are the basics: writing a
class, protecting the data inside the class, providing functions that allow the data inside
the class to be worked with, instantiating objects of the class, being able to call the
class’s functions both to access its data and to be able to activate different abilities
that objects of that class happen to have. Overall, in my opinion, it ends up making
for a fairly clean solution. I think object-oriented programming is often a very nice way to be
able to put together your projects.

Tags: , , , , , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *