PHP Class constructor question

Posted on 2006-04-26
Last Modified: 2012-06-27
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:

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

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() {

Thanks in advance for your time
Question by:GiantMatrix
    1 Comment
    LVL 2

    Accepted Solution

    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.


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now