View Single Post
Old 02-21-2008, 02:55 PM   #6 (permalink)
SOCK
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

I'm glad it worked out.

1) The constructor essentially 'sets up' the object with any default values or calls any methods that are crucial to the object's existence. So if you do have data members you need to set to a default state, or a class method that needs to perform a certain task prior to using the object, this is the place to do it. Constructors are OOP basics.

2) Yes. PHP v5 introduced the __construct() method as the default constructor. Prior to v5, it was the same name as the class (which is pretty normal for OOP). So in other words, these two are the same:

v4
PHP Code:
class MyTestClass {
  
// v4 data member, public by default
  
var $testme;

  
// v4 constructor
  
function MyTestClass($t) {
    
$this->testme $t;
  }

and v5
PHP Code:
class MyTestClass {
  
// v5 data member, setting visibility
  
protected $testme;

  
// v5 constructor
  
function __construct($t) {
    
$this->testme $t;
  }

As I stated before, you should always have a constructor on non-statically called classes, it's just good practice.

3) Not necessarily. Destructors are used for specific tasks when 'destroying' the object (when the script ends or when you specifically unset the object variable). It's like a constructor in reverse. You probably won't find it all that useful until you are well into OOP programming.

If you haven't already, I'd advise reading up more on objects and OOP in general. Just check Wikipedia for OOP or Google.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote