Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Classes and Objects Creating a Class and Defining an Object

Object Oriented

create an object instant to a class

fish.php
<?php
class Fish {
}
$p = bluegill();
?>

2 Answers

I am glad you asked this question because I did not remember the answer. After rewatching the video, I came up with this that passed.

<?php
class Fish {

}

$bluegill = new Fish();
?>

Creating an instance of the class makes the object a new instance of the class. So, object ($bluegill) is (=) a "new" instance of the class (fish()).

Kevin Korte
Kevin Korte
28,149 Points

First, don't forget your colon after your fish class.

Second, to create an object that is an instance, you're basically creating a variable with the dollar sign, and setting it to equal your new class name, using the new keyword.

There is no ; after the class. It is formatted like this:

class Fish {
} 

or

class Fish
{
}
Kevin Korte
Kevin Korte
28,149 Points

While true, it is optional, and out of habit I have been putting semi colons after classes. I didn't realize how optional it is, however it does not effect the outcome one way or another. I'll probably start working to break that habit and leaving it off.

I don't recall which course it was, but one of them said not to put a semicolon after curly braces. I cannot think of a time in PHP, CSS, Sass, or JavaScript where one is required after curly braces.