Re: how to pass variable argument list in a childconstructor to parent constructor, ideas wanted

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I don't understand completely your problem, but think that something
like this may help you:

<?php
class A
{
        function __construct()
        {
                var_dump(func_get_args());
        }
}

class B extends A
{
        function __construct()
        {
                $args = func_get_args();
                call_user_func_array(array(parent, '__construct'), $args);
        }
}

$a = new B(1, 2, 3);
?>

This will output:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

As expected.

The key is using call_user_func_array in combination with
func_get_args to call the parent constructor without knowing the
number of parameters.

Let me know if that helps.

Jonathan

On Sun, Aug 2, 2009 at 10:40 PM, Ralph Deffke<ralph_deffke@xxxxxxxx> wrote:
> problem:
>
> __ construct( $something ... variable argument list ){
>    parent::__construct( ??? );
> }
>
> I tried with func_get_args, but the problem is that it gives an indexed
> array back.
>
> lets say the argument list is mixed like this:
> ( "string", "string", array)
>
> func_get_args will give this
> [0] => "string"
> [1] => "string"
> [2] => array
>
> the parent constructor works also with the func:_get_args function in ordfer
> to handle variable argument lists
>
> if u pass the array from func_get_args the parent constructor will reveive
> after his func_get_args the following
>
> array( [0] => array( [0] => "string" [1] =>"string" [2] => array( ....)
>
> ok no problem so far. I thought just strip the key column of the array with
> a function like this
>
> function stripFirstColumn( $_fromThisArray ){
>    $b = "";
>    $_b = array();
>    foreach( $_fromThisArray as $value){
>      if( !is_array($value) ){
>  if( empty( $b )){
>   $b = $value;
>   //   $_b[$b]="";
>  } else {
>   $_b[$b]=$value;
>   $b = "";
>  }
>      } else {
>  array_push($_b, $value);  or $_b[] = $value;
>      }
>    }
>    return $_b;
> }
>
> BUT this results in an arry like this
>
> array( "string", "string", [0] => array( ...))
>
> HOWEVER u can create a propper array for this purpose by writing this :
>
> $_a = array( "string", "string", Array( "string" => "string",
> "string"=>"string"));
>
> but there is no way to create the same construction by code unless I use the
> eval() function what limits me to string objects only.
>
> im really a senior coder (since 1982) but this gives me a headace. what do u
> think?
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux