©
本文档使用
php.cn手册 发布
[#1] turald at gmail dot com [2015-10-16 13:56:55]
To be fair I have done this before.
[#2] turald at gmail dot com [2015-10-16 13:56:50]
To be fair I have done this before.
[#3] php dot spamguard at pulseforce dot com [2009-01-27 18:31:04]
PHP is great in how it allows you to send a variable-length argument list to a function by using the func_* functions. However, unfortunately (and this has been brought up numerous time in PHP bug reports/feature requests), it doesn't support passing arguments by reference which would have greatly simplified processing multiple variables at once by simply doing: somefunction($a, $b, $c) and having them processed, without having to do any additional assignment.
However, I've discovered a way to leverage several features of PHP and created a very good example that shows how you can process multiple variables in one fell swoop!
The usage is (with my example function/class):
"list($a, $b, $c, $d, $e) = TextEscape::escape_multi($a, $b, $c, $d, $e);"
If PHP had ACTUALLY been capable of passing variables by reference when using variable-length argument lists, that could have been reduced to:
"TextEscape::escape_multi($a, $b, $c, $d, $e);"
It's extremely close though, both in simplicity and speed. Just be VERY sure to ALWAYS have the SAME NUMBER AND ORDER of arguments on both sides of that statement.
The use of a static class was just to make the code cleaner and easier to look at. This can be applied to any functions!
Now, onto the code, enjoy the sheer brilliance and enjoy modifying multiple variables at once in just ONE statement! ;-)
<?php
class TextEscape
{
public static function escape_string($sString)
{
return htmlspecialchars($sString);
}
public static function escape_multi()
{
$aVariables = func_get_args();
foreach ($aVariables as $iKey => $sValue)
{
$aVariables[$iKey] = TextEscape::escape_string($sValue);
}
return $aVariables;
}
}
// Create test variables
$a = "A<bar";
$b = "B>bar";
$c = "C<bar";
$d = "D>bar";
$e = "E<bar";
// Variables Before:
// Array
// (
// [0] => A<bar
// [1] => B>bar
// [2] => C<bar
// [3] => D>bar
// [4] => E<bar
// )
print_r(array($a, $b, $c, $d, $e));
// Process every variable at once,
// we're exploiting both how list() and
// variable-length argument lists work
// to make the usage this smooth! ;-)
list($a, $b, $c, $d, $e) = TextEscape::escape_multi($a, $b, $c, $d, $e);
// Variables After:
// Array
// (
// [0] => A<bar
// [1] => B>bar
// [2] => C<bar
// [3] => D>bar
// [4] => E<bar
// )
print_r(array($a, $b, $c, $d, $e));
?>
That's the CLEAN LOOKING version of the code, which illustrates exactly what is HAPPENING.
I also made a speedfreak edition of the function, but didn't want to post it as the main example since it's harder to read.
But it's faster, much faster! It passes by reference where it can and loops in a more efficient way.
Just replace the above function with this one, the outcome is the same but the speed is better:
<?php
class TextEscape
{
public static function escape_string(&$sString) // CHANGED: we now pass by reference to the actual text processing function
{
$sString = htmlspecialchars($sString);
}
public static function escape_multi()
{
$aVariables = func_get_args();
for ($i=(count($aVariables)-1); $i>=0; $i--) // CHANGED: a more efficient for-loop is used instead of a foreach; and for anyone wondering about the counter declaration, this is a common optimization trick which makes it so that the size of the array only has to be checked ONCE, by initializing the counter to the index of the last element (which is size of array minus 1) and then iterating downwards until we reach 0 (the first element)
{
TextEscape::escape_string($aVariables[$i]); // CHANGED: this has been changed to modify the variable by reference instead
}
return $aVariables;
}
}
?>
Enjoy!
[#4] Michael T. McGrew [2005-05-12 09:56:55]
An interesting way to use functions is by putting other things than PHP in them, i.e. HTML CSS. This can be useful if you will be using a format for many pages. Yes, the include function can be used also, but by defining them in functions can make them more portable in a way and keep you from having to make many include files. You could use include() when one will be putting big portions of PHP, HTML, and/or CSS and will be using them in many pages. Then you could use PHP, HTML, and/or CSS in a function when it is smaller portions of code and only want it to be on a few pages.
I have been using include() for HTML and CSS in my early days of PHP coding, but I will be using functions for that a lot more now.
<?php function a() { ?>
<style type="text/css">
table {
border: 1px dashed black;
background: #95EAFE;
text-align:left;
width:610px;
}
.linkbar {
font-family:sans-serif;
line-height:40px;
top:1px;
text-align:center;
width:200px;
height:10px;
}
</style>
<?php } ?>
Now doing:
<?php a(); ?>
will return all of the CSS just as if you had wrote it on the actual page, or used include() . The same thing goes for HTML code inside a function. The possibilities are endless...
[#5] michael dot bommarito at gmail dot com [2005-01-30 11:28:58]
I've developed an easy-to-use hack using these functions to emulate overloaded functions. It's especially handy for constructors and/or C++ style templating.
Here's a little example to get you going. This does the trick for most circumstances, and if you need more intelligent parsing, it's not too hard to implement with regex or a more suitable classification scheme.
N.B. Note the lack of whitespace between variable types in case strings.
class Matrix {
...
function Matrix() {
$args = func_get_args();
$argmatch = implode(",", array_map('gettype', $args));
switch( $argmatch ) {
case 'integer,integer':
//initialize m x n matrix
break;
case 'integer,integer,integer':
//initialize m x n matrix with constant c
break;
case 'integer,integer,float':
//initialize m x n matrix with constant c
break;
case 'array':
//initialize from array (2D....)
break;
case 'array,integer':
//intialize from array (1D packed with m rows)
break;
default:
//(void) overload?
//error handling?
break;
}
}
...
}
[#6] php-note-2003-june-18 at ryandesign dot com [2003-06-18 08:22:31]
Xavier's example is rather complicated, and his task would be much more simply accomplished by using classes. Define a base class to do the basic functions open, dump, and close, and create extension classes to override whatever behavior.
class foo {
function open() {
// Default functionality for open()
}
function dump() {
// Default functionality for dump()
}
function close() {
// Default functionality for close()
}
}
class bar extends foo {
function open() {
// Override functionality of open()
}
// dump() and close() remain as in class foo
}
[#7] xmontero at dsitelecom dot com [2002-08-23 14:49:03]
You can do some kind of overloading functions in PHP using "function_exists".
Let's suppose you want a script to call plugins that are php files and you want the plugins to "override" behaviours that if not overriden are used by default.
You might for example write a "mySQL table viewer" which displays a table and it "opens a table", "dumps the header", "dumps row-by-row" and then "closes the table".
Let's suppose you want a plugin for "Stocks" which prints negative stocks in red, so you want to override the "dumping row-by-row" to do that behaviour. Instead you do not want to have all the "default behaviour" repeated in the plugin.
You may then do the following:
1) Define a default plugin (ex: "default.php"
2) Write all your functions in default.php to open, dump and close, but add them a suffix:
open_table_default()
dump_header_default()
dump_row_default()
close_table_default()
3) Call your functions with a wrapper: Insetad of this:
open_table() or open_table_default() write this:
plugincall("open_table");
4) Then write a function called plugincall which uses function_exists() to see if you call one or the other function:
function plugincall($desired_function_name)
{
if( function_exists( $desired_function_name))
{
//Call the plugin
//Note the $ before the name
$desired_function_name()
}
else
{
$desired_function_name = $desired_function_name . "_default";
if( function_exists($desired_function_name))
{
//Call the default
//Note the $ before the name
$desired_function_name()
}
else
{
// Nor the plugin nor the default were defined
}
}
So, now, if the require_once(plugin.php) contains a function called "open_table()", it will be used. Instaed, "open_table_default()" will be used.
It's not like overloading functions, but it is very useful to write scripts ampliable by other programmers: If they write a function, it is used, if not, the default is used.
See ya!
Xavier Montero.