#native_company# #native_desc#
#native_cta#

Variable argument lists and dynamic instantiation

By Steve Long
on May 10, 2001

<html>

<?php

/*

* 2001-03-16

*

* Steven P. Long

* [email protected]

*

*

* Illustrates the use of the func_ functions for

* processing variable argument lists in PHP4

*

* Also illustrates dynamic object invocations.

*

*/

//

// Let’s make a simple Widget class

//

class Widget {

var $a;

function Widget()

{

$this->a = array( “one”, 2.0, “III”) ;

echo “
new widget” ;

}

function bar( $s = “bar” ) { return $s ; }

function get_a() { return 49 ; }

function get_b() { return $this->a ; }

}

//

// Here is the function receiving the variable argument list.

// The argument list may vary in both number and types.

// foo recursively processes arrays and call all methods on

// objects (for illustration only, of course).

//

function foo()

{

// let’s get the argument list and number of arguments.

$args = func_get_args(); // this is the argument list

$nArgs = func_num_args(); // this is the number of arugments

// iterate through the argument list

for ( $i = 0; $i < $nArgs; ++$i )

{

// get the base type of this parameter [argument]

$bType = strtolower( gettype( $args[$i] ));

switch ( $bType ) // process based on argument’s type

{

case “string”:

case “integer”:

case “double”:

echo “
$bType ” . $args[$i] ;

break;

case “array”: // process arrays recursively

$a = $args[$i];

foreach ( $a as $key => $item )

{

echo “
recursing on foo for item [$i][$key]” ;

foo( $item );

}

break;

case “object”: // call a class method and create a new object

echo “
type $bType”;

$args[$i]->bar(“old-” . $i); // call object->bar()

$cName = get_class( $args[$i] ); // get the class name

$object = new $cName(); // make a new object

$object->bar(“new-” . $i); // call the new object->bar()

// now iterate over the methods of $cName and recurse on returns

$method = get_class_methods( $cName );

for ( $m = 0; $m < count( $method ); ++$m )

{

$mName = $method[$m];

if ( $mName != $cName )

{

$x = $object->$mName();

echo “
type(x) = ” . gettype( $x ) . ” :: ” . $x;

foo( $x );

}

}

break;

default:

echo “
$bType handled by default.” ; // all others here

}

echo “
“;

}

}

//

// This section is the “driver” portion for foo.

// Here, the variables are defines and foo is called.

//

// create parameters for foo()

$str = “aString”; // string

$chr = ‘a’; // character

$dbl = 9.87; // double (floating point)

$num = 654; // integer

$exp = 3.21e-2; // double (exponential)

$hex = 0xFF; // integer (hexadecimal)

$wdg = new Widget(); // object of type Widget

$ar1 = array( $dbl, $str, $wdg, $hex ); // array of mixed types

$ar2 = array( $chr, $ar1, $num); // another array of mixed types

// let’s try an array of arrays and some other things

$ar3 = array ( 0 => $ar1

, ‘a’ => $ar2

, 2 => $wdg

, ‘b’ => $num

);

// call foo() with varying number and type of parameters

foo( $str, $dbl, $chr, $ar1 );

foo( $chr, $ar2, $wdg, $hex, $exp, $ar3 );

?>

</html>