note 70502 added to function.array-slice
| From: | david at bagnara dot org | Date: | Thu, 19 Oct 2006 03:42:27 +0000 |
| Subject: | note 70502 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118737@lists.php.net to get a copy of this message | ||
I was trying to pass an argument list through the constructors. I tried various things such as
func_get_args(). My conclusion is to pass the args to the constructor as an array. Each constructor
can remove the fields it wants and pass the array on.
Using the following prototype, each child class can have any number of parameters added to the
beginning of the class constructor and the rest passed onto the parent.
If the default value is desired for an argument, just pass NULL.
This could possibly be better done with array_shift or the like.
<?php
class aChild extends aParent
{
// TODO customise this list for this class
public
$a, $b, $c;
function __construct( $args = array() )
{
//set up default values for this class
// TODO customise this list for this class
$default = array( "a-def", "b-def", "c-def" ) ;
// now overwrite the default with non NULL args
foreach( $args as $key=>$val )
{
// more args than needed?
if( !isset( $default[$key] ) )
{
break;
}
// this arg not null
if( isset( $val ) )
{
$default[$key] = $val ;
}
}
// set this to the new values
// TODO customise this list for this class
list( $this->a, $this->b, $this->c ) = $default ;
// take off the ones we used
$args = array_slice( $args, count( $default ) ) ;
parent::__construct( $args ) ;
}
}
$x = new aChild( array( "aChild a", NULL, "aChild c", NULL, "aParent
second", "aParent third" ) ) ;
?>
----
Server IP: 203.220.0.234
Probable Submitter: 220.237.187.216
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- https://master.php.net/note/edit/70502
Del: integrated -- https://master.php.net/note/delete/70502/integrated
Del: useless -- https://master.php.net/note/delete/70502/useless
Del: bad code -- https://master.php.net/note/delete/70502/bad+code
Del: spam -- https://master.php.net/note/delete/70502/spam
Del: non-english -- https://master.php.net/note/delete/70502/non-english
Del: in docs -- https://master.php.net/note/delete/70502/in+docs
Del: other reasons-- https://master.php.net/note/delete/70502
Reject -- https://master.php.net/note/reject/70502
Search -- https://master.php.net/manage/user-notes.php