knowNothing Posted October 17, 2006 Share Posted October 17, 2006 I've been doing top-down php dev for a while and I'm trying to convert everything I have into objects for well, reusability. I'm having trouble trying to decide what the purpose of a constructor function should do. I realize it basically needs to initialize the class with default values when instantiating it, but I'm not so sure I need to have default values specified? Am I supposed to be able to pass vaiables into this constructor function? I see what functionality a constructor function is supposed to have but I'm having a hell of a time trying to figure out how to apply it to my specific class.I'm writing an app for an online student locker system that takes care of the following:1. Student/Teacher Auth2. Private messaging3. File Upload (Each student gets his or her own filespace)4. Homework lists and downloadsThis is the basic functionality of the whole app. I am knowledgable enough to code all this in top-down form (don't laugh^^), but I would like to start off the project by designing objects for each of these items. (To save myself 100's of hours of maintenance.If I were to write a single class to accomplish this, what might the constructor function do? If a single class isn't the best way (which I'm starting to believe is true) What might constructor functions do for each of these objects? If you were quick to realize that maybe I'm going about this all wrong, please let me know the best way to start breaking these down into objects.Thanks for all your help with my previous, more specific php questions.Dan the knowNothing Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/ Share on other sites More sharing options...
Undead_Zeus Posted October 18, 2006 Share Posted October 18, 2006 If you don't have any values that need to be specified, simpily do not specify a constructor.Constructor methods can be overloaded. Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-110486 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 You don't need a constructor. If it is eg just a class consisting of random functions Then it is not necessary.[quote author=knowNothing link=topic=111785.msg453182#msg453182 date=1161099622]Am I supposed to be able to pass vaiables into this constructor[/quote]I'm not sure what you mean, but this is possible (if that's what you mean): [code]$hello = new hello("hi");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-110498 Share on other sites More sharing options...
roopurt18 Posted October 18, 2006 Share Posted October 18, 2006 It depends on what the class is designed to do. Let's say you are creating a universal DB class to handle MySQL interaction for all of your PHP projects.[code]<?phpclass MyDB{ var $host, $user, $pw; // The constructor, takes arguments function MyDB($host, $user, $pw){ $this->host = $host; $this->user = $user; $this->pw = $pw; $this->Connect(); } // Connect // Try to establish a DB connection function Connect(); // do stuff }}?>[/code]You would add all sorts of functionality to that class such as queries, caching, logging, etc. Then you can easily use it across projects like so:[code]<?php $DB = new MyDB('localhost', 'user', 'pw');?>[/code]You also have the added benefit of being able to expand your DB class for projects that have specific functionality. Let's say that your current application requires some form of DB interaction that differs from the rest of your sites; you can extend that class:[code]<?php require_once('MyDB.php'); class NewMyDB extends MyDB{ function NewMyDB(){ MyDB::MyDB('localhost', 'user', 'pw'); } // Give this class new functionality not available to other applications function NewFunctionality(){ // Do something } }?>[/code]You would use this class like so:[code]<?php $DB = new NewMyDB(); $DB->NewFunctionality();?>[/code]Keep in mind constructors are only meant for classes meant to be instantiated. So when designing your constructor, the basic question is what does this object need to know to begin functioning?As far as passing values into your constructor, sometimes that necessary. But consider the two scenarios:Scenario #1:Pull data from DBCreate object passing DB data from previous step into the constructorUse the objectScenario #2:Create object-- Object's constructor pulls data from the DB and initializes itselfUse the objectScenario #2 is almost always the more prefered method, although not always possible. Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-110801 Share on other sites More sharing options...
Jenk Posted October 18, 2006 Share Posted October 18, 2006 Couple of corrections to the above php4 code (sorry, I'm pedantic..)The call to the parent constrcutor is static in that code, it shouldn't be - use $this->MyDB() instead.when assigning objects, always use reference assignment (=&) unless you actually want to create a clone. Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-110814 Share on other sites More sharing options...
roopurt18 Posted October 18, 2006 Share Posted October 18, 2006 Thanks for the corrections! I come from a C++ background where things are slightly different. Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-110818 Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 You do not need to use =& Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-111067 Share on other sites More sharing options...
neylitalo Posted October 19, 2006 Share Posted October 19, 2006 [quote author=Daniel0 link=topic=111785.msg454175#msg454175 date=1161239013]You do not need to use =&[/quote]That's only the case in PHP 5. In PHP 4, objects are passed by value as opposed to by reference as they are in PHP 5, so =& is indeed required in PHP 4 in order if you want to keep using the same object. Quote Link to comment https://forums.phpfreaks.com/topic/24223-purpose-of-class-constructor-and-how-to-decide-what-objects-are-needed/#findComment-111270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.