This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses 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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up