PHP Class constructor question

Hi All,

I have a couple of questions on PHP class constructors.

A. I am building an application under PHP4 and AFAIK when writing the class constructor function in PHP 4, I should give it the same name as the class name. But in PHP5, I should use something like _construct() function. In other words:

PHP5:
class MyClass {
  public function _construct() {
    // instantiate stuff here
  }
}

PHP4:
class MyClass {
  public function MyClass() {
    // instantiate stuff here
  }
}

Now my question is if I build the application under PHP4 and I'm going to use it on a server running PHP5, shall I change the construct function in all of my classes or will it work with the PHP4 construct functions?

B. I need to put a code in my class which will be executed once I write:
$ClassObj = &New MyClass;

So is the following code correct?

class DBCls
{
//------------------------------------------------------------------------------
function DBCls() {
    ConnectToDB();
}
...
//------------------------------------------------------------------------------


Thanks in advance for your time
GiantMatrixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gschwaCommented:
Actually it is __construct in PHP5, that's two underscores.  But other then that you are more or less right.  

In regards to your question, you can leave the constructor as the class name -- PHP5 will support that.  

Here's a test that I did on PHP5 and it worked correctly.

<?php

class MyTest
{
    var $var = 0;
   
    function MyTest()
    {
        $this->var = 5;
    }

    function get()
    {
        return $this->var;
    }
}

$t = new MyTest();

print $t->get();

?>

However I would recommend if you have access to PHP5 you actually develop it to PHP5 since it has alot better object support than does PHP4.  Also in PHP5 you don't have to deal with passing the objects by reference, as you do in PHP4, since they are always passed by reference.  Additionally, the exception handling is much better.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.