Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Elegant way of handling nested constructors with variable argument lists?

    Hi all,

    I wonder if there is an elegant (i.e. syntactic way) of passing variable argument lists between nested constructors (or any function), for that matter.

    The problem which arises is as follows:

    PHP Code:

    class mybase
    {
      public function 
    __construct()
        {
          
    $argc func_num_args();
          
    $argv func_get_args();

          
    //Do something here with argv and argc.
        
    }
    }

    class 
    myderived extends mybase
    {
      public function 
    __construct()
        {
          
    parent::__construct();  //Here is the problem!

          //Do something here.
        
    }

    I can think of various solutions (I prefer to call them hacks!), the most obvious of which is to write a protected function to be called from mybase::__construct() which wraps the function args into an array anyway (so that they can be wrapped into an array and passed directly to that underlying function in the constructor of the derived class), however I would put that particular solution into the category of "ugly"!

    I wonder if there is a mechanism that is built into PHP5 for doing this ... in C, for example, using the stdarg library (in a posix environment) forces the handling of variable argument lists using a separate language construct, which can be recursively passed to functions (a neat solution!).

    Mike
    Last edited by mlse; 08-17-2007 at 01:09 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you could do something like:
    PHP Code:
    class derived extends base {
        public function 
    __construct() {
            
    $args func_get_args();
            
    call_user_func_array(array(parent,'__construct'),$args);
        }
        

    but it's still not particularly nice. What's the use-case for variable arguments to a constructor that can't be handled with optionals?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    None really! I've just gotten used to writing C for so long that I prefer variable arg lists to optionals! Also, I have one particular base class which I'd like to be able to initialise with an arbitrary number of arguments.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •