2 Replies - 315 Views - Last Post: 02 September 2012 - 01:51 PM Rate Topic: -----

#1 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 88
  • Joined: 26-April 12

NEWBIE DOUBT about classes

Posted 02 September 2012 - 10:33 AM

Hi everyone!

i have a doubt about constructors, wich way is the correct one to set up a constructor

   
   class example
      {
           public function example()
                 {
                   //some code
                 }
      }

// OR
   
   class example
      {
           public function __construct()
                 {
                   //some code
                 }
      }




Is This A Good Question/Topic? 0
  • +

Replies To: NEWBIE DOUBT about classes

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: NEWBIE DOUBT about classes

Posted 02 September 2012 - 10:59 AM

*
POPULAR

Depends on your PHP version. Your first version is how it was done in PHP4. The second version is how we currently do it in PHP5.

These days you should have long since moved on from PHP4, so the second version is what you should be doing. PHP5 still allows the PHP4 syntax, but only for backwards compatibility.

http://www.php.net/m....oop5.decon.php

php.net said:

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.

This post has been edited by Atli: 02 September 2012 - 03:16 PM

Was This Post Helpful? 6
  • +
  • -

#3 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 88
  • Joined: 26-April 12

Re: NEWBIE DOUBT about classes

Posted 02 September 2012 - 01:51 PM

View PostAtli, on 02 September 2012 - 10:59 AM, said:

Depends on your PHP version. The first version is how it was done in PHP4. The second version is how we currently do it in PHP5.

These days you should have long since moved on from PHP4, so the second version is what you should be doing. PHP5 still allows the PHP4 syntax, but only for backwards compatibility.

http://www.php.net/m....oop5.decon.php

php.net said:

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.


Thank you Atli.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1