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) 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.