Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Class and Method Interaction
I'm finishing the introduction about POO in PHP but there is someting I don't get. When creation an object we do this:
$soda = new Product(); /* we use parenthesis in this case.*/
But in the video here, Paulk shows a little trick. He does this:
$class = "Product"; $p = new $class; /* in other words $p = new Product*/ /*But why don't we need to specify the parenthesis, why not doing this ? */ $class = "Product()"; $p = new $class; /* I haven't tested yet, as where I'm atm, It's not possible.*/
Chris Shaw26,650 Points
The parenthesis while they are good practise to use aren't required for classes that don't require any arguments for the constructor, so for example all 3 of the below constructors are exactly the same when the constructor runs.
<?php $p = new Product(); $p = new Product; $class = 'Product'; $p = new $class;
The last example you have above won't work as the parenthesis are invalid and can't be evaluated as a class name because there is no such class called
Product() which is impossible anyway.
Hope that helps.