Jump to content

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


Recommended Posts

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

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]
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]
<?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
  }
}
?>
[/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 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.
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 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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.