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 trialPresley Cobb
9,214 PointsCan anyone better explain what the keyword new does?
The video explains that you need to use the keyword new when creating an object but does not explain why. In later videos you use the keyword new but it's not clear why it's needed or what happens if you leave it out.
4 Answers
Ted Sumner
Courses Plus Student 17,967 PointsIf you don't use new your code will probably break. PHP will think you are assigning a new variable and look for the typical variable types. Once you assign it to a class with new, then you can invoke the class methods and properties.
Ted Sumner
Courses Plus Student 17,967 Pointsa class is a chunk of code you want to use over and over. Each time you want to use it you have a new use, which allows new info to be passed. Each instance of your object can store its own parameters with the same name at the same time because each instance is a new version of using the code.
Presley Cobb
9,214 PointsSo if I dont use the "new" keyword then will the new object use only default data(assume I assign default data)? Or will the code just break entirely?
Presley Cobb
9,214 PointsThanks!