auto construct parent class

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have an AppController and some classes that extend the controller.

class UsersController extends AppController
    function UsersController()


1) Is it the same using function UsersController() {} and function
_construct() {} ??

2) Is there a way not to have in every controller the
function UsersController()
Or in other way, is it possible for the parent construction function to
be executed automatically?


Re: auto construct parent class

Harris Kosmidhs wrote:
Quoted text here. Click to load it


Iirc Not,

Child constructor overrides the parent constructor making it not
construct :-)

You could do something smart with autoload or whater u use but the
constructor will only be called once.

function __autoload($cls){


Currently working on

Re: auto construct parent class

On Wed, 22 Oct 2008 11:04:09 +0200, Harris Kosmidhs  

Quoted text here. Click to load it

__construct(), but yeah, for all practical purposes they are the same.

Quoted text here. Click to load it

1) As long as you're using PHP5 (PHP4 has been EOL for a while...), just  
use __construct(), it will make your live easier.
2) Although the function name is different, the parent constructor will be  
called if you haven't defined a constructor in your child class, without  
you having to specify it. For example:
class Foo{
    function Foo(){
        echo __CLASS__.':'.__FUNCTION__.' called';
class Bar extends Foo{}
new Bar();
Foo:Foo called

3) If you override the constructor, there is no other way to run the  
parents constructor save for putting it explicitly (i.e.  
parent::__construct()) in your new constructor.

4) Often, when a constructor performs a lot of statement, people choose to  
move either the code that differs (or the code that remains the same) to  
another function (init() for example), which get's called from the  
constructor, which you can override or keep as you wish rather then extend  
the constructor. In my opinion that's largely a matter of taste.
Rik Wasmus

Site Timeline