how to pass variable argument list in a childconstructor to parent constructor, ideas wanted
how to pass variable argument list in a childconstructor to parent constructor, ideas wanted
am 03.08.2009 03:40:46 von Ralph Deffke
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
Re: how to pass variable argument list in a childconstructor to
am 03.08.2009 04:16:47 von Jonathan Tapicer
I don't understand completely your problem, but think that something
like this may help you:
class A
{
function __construct()
{
var_dump(func_get_args());
}
}
class B extends A
{
function __construct()
{
$args =3D func_get_args();
call_user_func_array(array(parent, '__construct'), $args);
}
}
$a =3D new B(1, 2, 3);
?>
This will output:
array(3) {
[0]=3D>
int(1)
[1]=3D>
int(2)
[2]=3D>
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 wrote:
> problem:
>
> __ construct( $something ... variable argument list ){
> =A0 =A0parent::__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] =3D> "string"
> [1] =3D> "string"
> [2] =3D> array
>
> the parent constructor works also with the func:_get_args function in ord=
fer
> to handle variable argument lists
>
> if u pass the array from func_get_args the parent constructor will reveiv=
e
> after his func_get_args the following
>
> array( [0] =3D> array( [0] =3D> "string" [1] =3D>"string" [2] =3D> array(=
....)
>
> ok no problem so far. I thought just strip the key column of the array wi=
th
> a function like this
>
> function stripFirstColumn( $_fromThisArray ){
> =A0 =A0$b =3D "";
> =A0 =A0$_b =3D array();
> =A0 =A0foreach( $_fromThisArray as $value){
> =A0 =A0 =A0if( !is_array($value) ){
> =A0if( empty( $b )){
> =A0 $b =3D $value;
> =A0 // =A0 $_b[$b]=3D"";
> =A0} else {
> =A0 $_b[$b]=3D$value;
> =A0 $b =3D "";
> =A0}
> =A0 =A0 =A0} else {
> =A0array_push($_b, $value); =A0or $_b[] =3D $value;
> =A0 =A0 =A0}
> =A0 =A0}
> =A0 =A0return $_b;
> }
>
> BUT this results in an arry like this
>
> array( "string", "string", [0] =3D> array( ...))
>
> HOWEVER u can create a propper array for this purpose by writing this :
>
> $_a =3D array( "string", "string", Array( "string" =3D> "string",
> "string"=3D>"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 d=
o 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
RE: how to pass variable argument list in a childconstructor to parent constructor, ideas wanted
am 03.08.2009 12:46:19 von M.Ford
> -----Original Message-----
> From: Ralph Deffke [mailto:ralph_deffke@yahoo.de]
> Sent: 03 August 2009 02:41
>=20
> problem:
>=20
> __ construct( $something ... variable argument list ){
> parent::__construct( ??? );
> }
>=20
> I tried with func_get_args, but the problem is that it gives an
> indexed
> array back.
>=20
> lets say the argument list is mixed like this:
> ( "string", "string", array)
>=20
> func_get_args will give this
> [0] =3D> "string"
> [1] =3D> "string"
> [2] =3D> array
>=20
> the parent constructor works also with the func:_get_args function
> in ordfer
> to handle variable argument lists
>=20
> if u pass the array from func_get_args the parent constructor will
> reveive
> after his func_get_args the following
>=20
> array( [0] =3D> array( [0] =3D> "string" [1] =3D>"string" [2] =3D> array(
> ....)
>=20
> ok no problem so far. I thought just strip the key column of the
> array with
> a function like this
>=20
> function stripFirstColumn( $_fromThisArray ){
> $b =3D "";
> $_b =3D array();
> foreach( $_fromThisArray as $value){
> if( !is_array($value) ){
> if( empty( $b )){
> $b =3D $value;
> // $_b[$b]=3D"";
> } else {
> $_b[$b]=3D$value;
> $b =3D "";
> }
> } else {
> array_push($_b, $value); or $_b[] =3D $value;
> }
> }
> return $_b;
> }
>=20
> BUT this results in an arry like this
>=20
> array( "string", "string", [0] =3D> array( ...))
I may be missing something completely here, but aren't you just looking for
$stripped_array =3D $_fromThisArray[0];
Even if you have to invent a "dummy" real__construct method in the parent c=
lass to call from the extended classes, and then relay the parent __constru=
ct into it; something like:
class parent_class {
function __construct( $something ... variable argument list ){
real__construct(func_get_args());
}
protected function real__construct() {
$args =3D func_get_args();
$args =3D $args[0];
....
}
}
class extended_class extends parent_class {
function __construct() {
parent::real__construct(func_get_args());
}
}
Disclaimer: this is all off the top of my head before morning coffee, and c=
ompletely untested. Someone else will quite likely come up with a much bett=
er way to do this, as OOP is not really my forte... ;) ;)
Cheers!
Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php