auto construct parent class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harris Kosmidhs

    auto construct parent class

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

    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?

    thanks
  • Floortje

    #2
    Re: auto construct parent class

    Harris Kosmidhs wrote:
    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() {} ??
    Yep
    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?
    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(PA TH_CLS.$cls.'.c ls.php');
    if(method_exist s($class_name,' my_construct')) {
    call_user_func( array($cls,'my_ construct'));
    }
    }


    Floortje

    --
    Currently working on dogcetera.com

    Comment

    • Rik Wasmus

      #3
      Re: auto construct parent class

      On Wed, 22 Oct 2008 11:04:09 +0200, Harris Kosmidhs
      <hkosmidi@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

      Comment

      Working...