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.

PHP Object-Oriented PHP Basics (Retired) Properties and Methods Constructor Method

Arguments in construct

I see there is Arguments in __construct class like function __construct ($name,$price,$desc){ //code goes here } So my question is, 1.Can we construct a method without passing arguments? What is the use of arguments here?

  1. Will it ok, if we construct a method checking if database is connected or if the session is timed out or not?

1 Answer

If I understand the question correctly, here are my thoughts.

  1. you can have a __construct with no arguments - sometimes this is what you want, you can use the __construct method to call other methods if you like. The key is that the method runs every time you create a new object. Read this doc page for more info

  2. Yes, but more often you would want to create a new class that handles all of that then call a method from that class. Also you should look into ORMs like Doctrine.