note 98458 added to function.call-user-func-array
| From: | Kris dot Craig at gmail dot com | Date: | Wed, 16 Jun 2010 22:28:59 +0000 |
| Subject: | note 98458 added to function.call-user-func-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170188@lists.php.net to get a copy of this message | ||
Many people have wondered how to effectively implement dispatch tables in PHP. Here's my
answer to that (if you'll forgive my creative flair):
<?php
/*
* Using dispatch tables in PHP.
*
* --Kris Craig
*/
define( "YOUR_MOTHER", 1 );
define( "YOUR_FATHER", 2 );
define( "YOUR_BROTHER", 3 );
define( "YOUR_SISTER", 4 );
class MyFamily
{
static $dispatch = array( YOUR_MOTHER => "Mom", YOUR_FATHER =>
"GetPrisonInmate", YOUR_BROTHER => "ReplaceName", YOUR_SISTER =>
"LazyGirl" );
static $args = array( YOUR_MOTHER => array(), YOUR_FATHER => array( "55170-054",
TRUE ), YOUR_BROTHER => array(), YOUR_SISTER => array() );
function GetDispatch( $fromwhere )
{
return call_user_func_array( array( self, self::$dispatch[$fromwhere] ), self::$args[$fromwhere]
);
}
function Mom()
{
return "Mommy loves you!";
}
function GetPrisonInmate( $PrisonerID, $GoodBehavior )
{
//Check prison records for his ID, then....
if ( $GoodBehavior )
{
$parole = "APPROVED";
}
else
{
$parole = "DENIED";
}
return "Your father (#$PrisonerID) has $remaining years left in his sentence. His most
recent parole application has been: $parole";
}
function ReplaceName()
{
return "Her name is Sally now.";
}
function LazyGirl()
{
print "Your sister needs to get out more....";
//sleep( pow( 60, 2 ) * 18 ); //You can sleep later!
die( "Nah, I'm too tired." );
}
}
print "Status on family member: " . MyFamily::GetDispatch( YOUR_FATHER );
?>
----
Server IP: 64.71.164.2
Probable Submitter: 131.107.0.72
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func-array.php
Edit -- https://master.php.net/note/edit/98458
Del: integrated -- https://master.php.net/note/delete/98458/integrated
Del: useless -- https://master.php.net/note/delete/98458/useless
Del: bad code -- https://master.php.net/note/delete/98458/bad+code
Del: spam -- https://master.php.net/note/delete/98458/spam
Del: non-english -- https://master.php.net/note/delete/98458/non-english
Del: in docs -- https://master.php.net/note/delete/98458/in+docs
Del: other reasons-- https://master.php.net/note/delete/98458
Reject -- https://master.php.net/note/reject/98458
Search -- https://master.php.net/manage/user-notes.php