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
In order to create a class, we group the code that handles a certain topic into one place. For example, we can group all of the code that handles the creation of recipes in a cookbook into one class. In order to interact with a class, we need to create an object from that class.
Declaring a Class
We declare a class using the “class” keyword, followed by the name of the class and a set of curly braces. Although PHP doesn't really care about spacing, the standard is to start the curly braces on the next line down. Although class names are NOT case sensitive, changing case within your program can get extremely confusing.
class Recipe
{
}
StudlyCaps
The standard for naming classes is StudlyCaps, which means the first letter should be capitalized, as well as the first letter of any subsequent word, all other letters should be lower case. For example: in a class named “MyRecipe”, the M and the R would be capitalized and the other letters would be lower case.
Instantiating an Object
After creating the class, a new object can be instantiated and stored in a variable using the “new” keyword:
$recipe1 = new Recipe();
Challenge
Start grouping things around you into objects.
-
0:00
[MUSIC]
-
0:04
Welcome back.
-
0:05
I'm excited to start writing our first object.
-
0:08
Once you understand the syntax you'll be ready to start grouping
-
0:11
everything in your life into objects, which by the way,
-
0:14
is a great way to solidifying your understanding of objects.
-
0:18
This desk is an object with four legs and a top.
-
0:22
This laptop is an object with storage,
-
0:25
memory, operating system, and programs that perform actions.
-
0:30
And even I'm an object with hair color, eye color, height, weight, and gender.
-
0:35
And I perform actions, such as, smiling and teaching you to code.
-
0:40
Let's take a look at the syntax needed to create the recipe class, which will define
-
0:45
the object, then we use that class to create an actual recipe object.
-
0:51
There are a few conventions or
-
0:53
standards that are used when it comes to organizing files and creating classes.
-
0:58
Following these standards allows your code, to be more readable.
-
1:03
To start with,
-
1:04
although this is not a requirement there is typically only one class per file.
-
1:09
The file name is the same as the class name and
-
1:12
all class files are stored within a classes folder.
-
1:16
Let's go into work spaces and get started.
-
1:19
Let's create a folder.
-
1:22
We'll name it classes.
-
1:27
Then we'll create a new file named recipe.php.
-
1:33
Within this file we're going to start defining our recipe class.
-
1:38
The syntax to create a class is pretty straightforward.
-
1:41
After our opening php tag, we declare a class using the class keyword.
-
1:49
We follow this by the class name, And a set of curly braces.
-
1:57
Although php doesn't really care about spacing,
-
2:00
the standard is to start the curly braces on the next line down.
-
2:05
Although class names are not case sensitive,
-
2:08
changing case within your program can get extremely confusing.
-
2:12
The standard for naming classes is studly caps.
-
2:15
Which means that the first letter should be capitalized.
-
2:18
As well as the first letter of any subsequent word.
-
2:22
All other letters should be lowercase.
-
2:25
If I were to name this class, MyRecipe, the M and the R would be capitalized.
-
2:32
And the other letters would be lowercase.
-
2:35
After creating the class, a new object can be instantiated, and stored in a variable.
-
2:40
We'll name this recipe1.
-
2:44
We then use the new keyword and the name of our class, Recipe.
-
2:50
We use an opening and closing parentheses, and then end our line with a semicolon.
-
2:56
To see the contents of this new object, we use the var dump.
-
3:05
We could view this file in a browser but
-
3:07
we can also see the output right in the console.
-
3:10
If your console is not showing go to View > Show Console.
-
3:17
In the consul type php, a space,
-
3:20
the folder name, a forward slash and then the filename.
-
3:27
This tells us that our variable type is an object and
-
3:31
it's an instance of the class recipe.
-
3:35
Our object is currently empty [LAUGH] and it's not really useful yet.
-
3:39
But you've just completed your first object oriented script.
-
3:42
Congratulations.
You need to sign up for Treehouse in order to download course files.
Sign up