Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We’ll take the basic programming concepts you already know and show you how to think about software in terms of objects.
-
0:00
For many PHP programmers, object oriented programming is an intimidating concept,
-
0:05
because it introduces new syntax and seemingly complex terminology.
-
0:10
At a glance, object oriented programming can appear to be far more complex
-
0:15
than simple procedural or inline code.
-
0:18
However, this doesn't have to be the case.
-
0:21
As you'll see, object oriented programming
-
0:24
is actually similar to the way we perceive the real world.
-
0:28
That means an object oriented approach
-
0:30
can help you visualize the solution to a problem more easily.
-
0:34
Object oriented programming or OOP for short, is a style of coding that
-
0:39
allows developers to group similar tasks into containers.
-
0:43
In php, containers are defined using classes.
-
0:47
A class contains all the functions and variables that define an object.
-
0:53
These functions are called methods and the variables are called properties.
-
0:56
[SOUND] To give you a real world example of this, let's take a look at a house.
-
1:01
A house defined by a blueprint which shows the layout, rooms and
-
1:06
exact design for the house.
-
1:09
Each house has attributes like walls, a roof, windows and doors.
-
1:15
They also have actions such as open the door or turn on the light.
-
1:21
The blueprint could be the class.
-
1:23
It's not a house itself, but it describes how a house should be built,
-
1:27
with relationships between the different parts of the house clearly defined and
-
1:31
planned out.
-
1:33
The walls, roof, windows, and doors are properties.
-
1:37
While opening the door and turning on the lights are methods.
-
1:42
As I just mentioned a blueprint is not a house.
-
1:45
But you use a blueprint to build new houses.
-
1:49
Each time a new house is built, we create a new object, a house object.
-
1:55
If our blueprint is a class named house,
-
1:58
we can build an entire subdivision of houses based on this blueprint.
-
2:03
This is called instantiating an object or creating an instance of an object.
-
2:10
Each house object shares the properties and methods of the house class.
-
2:14
But the individual values for each house may be different.
-
2:19
My house may have brown walls and a red roof.
-
2:23
While your house could have white walls and a blue roof.
-
2:28
Grouping code into these containers, or classes, can make it easier to
-
2:32
follow the tenant, don't repeat yourself, or D-R-Y, dry for short.
-
2:37
One of the major benefits in following the DRY principle is that,
-
2:41
if a piece of information changes in your program,
-
2:44
you usually only need to change the code in one place.
-
2:48
One of the biggest nightmares for developers,
-
2:50
is maintaining code where data is declared over and over again.
-
2:56
If something goes wrong tracking down the error can be like looking for
-
3:00
a needle in a haystack.
-
3:01
As you hunt for duplicated data and
-
3:03
functionality, sometimes finding that needle can be painful.
-
3:08
This course is designed to teach you the why and
-
3:11
how of object oriented programming.
-
3:14
Before we jump feet first into the how,
-
3:16
let's get a little more familiar with the why.
You need to sign up for Treehouse in order to download course files.
Sign up