On Wed, 22 Oct 2008 11:04:09 +0200, Harris Kosmidhs
<hk******@remov e.me.softnet.tu c.grwrote:
Hello,
I have an AppController and some classes that extend the controller.
I.e:
class UsersController extends AppController
{
function UsersController ()
{
parent::AppCont roller();
}
.....
}
1) Is it the same using function UsersController () {} and function
_construct() {} ??
__construct(), but yeah, for all practical purposes they are the same.
2) Is there a way not to have in every controller the
function UsersController () {parent::AppCon troller();}
Or in other way, is it possible for the parent construction function to
be executed automatically?
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:
<?php
class Foo{
function Foo(){
echo __CLASS__.':'._ _FUNCTION__.' called';
}
}
class Bar extends Foo{}
new Bar();
?>
Output:
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::__const ruct()) 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