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 trialMUZ140996 Simbarashe Gombedza
3,813 PointsObject Oriented
create an object instant to a class
<?php
class Fish {
}
$p = bluegill();
?>
2 Answers
Ted Sumner
Courses Plus Student 17,967 PointsI 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
28,149 PointsFirst, 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.
Ted Sumner
Courses Plus Student 17,967 PointsThere is no ; after the class. It is formatted like this:
class Fish {
}
or
class Fish
{
}
Kevin Korte
28,149 PointsWhile 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.
Ted Sumner
Courses Plus Student 17,967 PointsI 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.