By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

auto construct parent class

P: n/a
Hello,

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

I.e:
class UsersController extends AppController
{
function UsersController()
{
parent::AppController();
}

.....
}

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() {parent::AppController();}
Or in other way, is it possible for the parent construction function to
be executed automatically?

thanks
Oct 22 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Harris Kosmidhs wrote:
Hello,

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

I.e:
class UsersController extends AppController
{
function UsersController()
{
parent::AppController();
}

.....
}

1) Is it the same using function UsersController() {} and function
_construct() {} ??
Yep
2) Is there a way not to have in every controller the
function UsersController() {parent::AppController();}
Or in other way, is it possible for the parent construction function to
be executed automatically?
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){
require_once(PATH_CLS.$cls.'.cls.php');
if(method_exists($class_name,'my_construct')){
call_user_func(array($cls,'my_construct'));
}
}
Floortje

--
Currently working on dogcetera.com
Oct 22 '08 #2

P: n/a
On Wed, 22 Oct 2008 11:04:09 +0200, Harris Kosmidhs
<hk******@remove.me.softnet.tuc.grwrote:
Hello,

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

I.e:
class UsersController extends AppController
{
function UsersController()
{
parent::AppController();
}

.....
}

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::AppController();}
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::__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
Oct 22 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.