Jump to content


Photo

Purpose of class constructor and how to decide what objects are needed


  • Please log in to reply
7 replies to this topic

#1 knowNothing

knowNothing
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 17 October 2006 - 03:40 PM

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 Auth
2.  Private messaging
3.  File Upload (Each student gets his or her own filespace)
4.  Homework lists and downloads

This 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



#2 Undead_Zeus

Undead_Zeus
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 October 2006 - 05:29 AM

If you don't have any values that need to be specified, simpily do not specify a constructor.

Constructor methods can be overloaded.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 06:41 AM

You don't need a constructor. If it is eg just a class consisting of random functions Then it is not necessary.

Am I supposed to be able to pass vaiables into this constructor

I'm not sure what you mean, but this is possible (if that's what you mean):
$hello = new hello("hi");


#4 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 18 October 2006 - 05:53 PM

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.

<?php
class 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
  }
}
?>

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:
<?php
  $DB = new MyDB('localhost', 'user', 'pw');
?>

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:
<?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
    }
  }
?>

You would use this class like so:
<?php
  $DB = new NewMyDB();
  $DB->NewFunctionality();
?>

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 DB
Create object passing DB data from previous step into the constructor
Use the object

Scenario #2:
Create object
-- Object's constructor pulls data from the DB and initializes itself
Use the object

Scenario #2 is almost always the more prefered method, although not always possible.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 18 October 2006 - 06:18 PM

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.

#6 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 18 October 2006 - 06:26 PM

Thanks for the corrections!  I come from a C++ background where things are slightly different.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 06:23 AM

You do not need to use =&

#8 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 19 October 2006 - 02:06 PM

You do not need to use =&


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.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users