Php function argument by name

Передача аргументов функции по имени в PHP

При рефакторинге кода возникла одна небольшая идея относительно вызова методов. Иногда возникает потребность передать функции аргумент по имени. Например тогда, когда невозможно (или неудобно) передать список в нужном порядке. Такими случаями могут быть вызовы динамических блоков из шаблонизаторов: в шаблоне у нас > и в коде есть следуйщая синатура функции — function foo($first, $second, $third, $fourth). подобный подход используеться в системе Magento для вызова блоков из лей-аутов; или нужно передать методу даные на основе фильтра в каком-нибуть асоциативном масиве. В PHP4 возможным решением было помещение всего списка аргументов в масив. В PHP версии 5 есть же Reflection API, с помощью которого возможно проделать подобное. Perl, Python (, . ) могут, так почему ж ето должно быть невозможно в PHP?:)

UPD: код на pastebin затерся, вот метод-хелпер для етого (вызов вида $object->__named(‘methodNameHere’, array(‘arg3’ => ‘three’, ‘arg1’ => ‘one’)))

/**
* Pass method arguments by name
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __named($method, array $args = array())
$reflection = new ReflectionMethod($ this , $method);

$pass = array();
foreach ($reflection->getParameters() as $param)
/* @var $param ReflectionParameter */
if (isset($args[$param->getName()]))
$pass[] = $args[$param->getName()];
>
else
$pass[] = $param->getDefaultValue();
>
>

return $reflection->invokeArgs($ this , $pass);
>

* This source code was highlighted with Source Code Highlighter .

Источник

PHP Named Arguments

Summary: in this tutorial, you will learn about PHP named arguments and how to use them effectively in your code.

Читайте также:  Python get instance address

Introduction to the PHP named arguments

Since PHP 8.0, you can use named arguments for functions. The named arguments allow you to pass arguments to a function based on the parameter names rather than the parameter positions.

The following example defines a function that finds the position of the first occurrence of a substring in a string:

 function find($needle, $haystack) < return strpos($haystack, $needle); > Code language: HTML, XML (xml)

To call the find() function, you pass the arguments based on the parameter positions like this:

find('awesome', 'PHP is awesome!');Code language: JavaScript (javascript)

In this case, $needle is ‘awesome’ and $haystack is ‘PHP is awesome!’ .

However, the function call is not apparent. For example, you don’t know which argument is the needle and which argument is the haystack.

Sometimes, you may accidentally make a mistake by passing the arguments in the wrong order. For example:

find ( 'PHP is awesome!', 'awesome' );Code language: JavaScript (javascript)

This is buggy and very difficult to troubleshoot.

To avoid this, you may add comments to the arguments like this:

find ( 'awesome', // needle 'PHP is awesome!' // haystack );Code language: JavaScript (javascript)

The comment makes the code more clear. However, it’s not robust.

To improve this, PHP 8.0 introduced the named arguments that allow you to specify the parameter names when passing arguments:

find ( $needle : 'awesome', $haystack : 'PHP is awesome!' );Code language: PHP (php)

Since you are using the parameter names, the positions are not necessary. For example, you can swap the parameters like this:

find( $haystack :'PHP is awesome!', $needle : 'awesome' );Code language: PHP (php)

Skipping default arguments

The following defines a function that creates an anchor element ( ) from text, href, title, and target:

 function create_anchor( $text, $href = '#', $title = '', $target = '_self' ) < $href = $href ? sprintf('href="%s"', $href) : ''; $title = $title ? sprintf('title="%s"', $title) : ''; $target = $target ? sprintf('target="%s"', $target) : ''; return "$text"; >Code language: HTML, XML (xml)

To create a link with the target is _blank , you must specify all the default arguments until the one you want to change. For example:

$link = create_anchor( 'PHP Tutorial', 'https://www.phptutorial.net/', '', '_blank' ); echo $link;Code language: PHP (php)
a href="https://www.phptutorial.net/" target="_blank">PHP Tutorial a> Code language: HTML, XML (xml)

In this example, you need to pass the space (”) to the third argument. If you use the named arguments, you don’t have to specify all the defaults. For example:

 $link = create_anchor( text : 'PHP Tutorial', href : 'https://www.phptutorial.net/', target: '_blank' );Code language: PHP (php)

Mixing named arguments with positional arguments

PHP allows you to call a function by using both positional arguments and named arguments. And you need to place the named arguments after positional arguments. For example:

$link = create_anchor( 'PHP Tutorial', 'https://www.phptutorial.net/', target: '_blank' );Code language: PHP (php)
  • The text is ‘PHP Tutorial’ .
  • The href is ‘https://www.phptutorial.net/’ .
  • And the target is ‘_blank’ .

If you place the named arguments before the positional arguments, you’ll get an error. For example:

create_anchor( target : '_blank', 'PHP Tutorial', 'https://www.phptutorial.net/' );Code language: JavaScript (javascript)
Cannot use positional argument after named argumentCode language: PHP (php)

Summary

  • Use PHP named arguments to pass arguments to a function based on the parameter names.
  • Put the named arguments after the positional arguments in function calls.

Источник

Php function argument by name

To experiment on performance of pass-by-reference and pass-by-value, I used this script. Conclusions are below.

#!/usr/bin/php
function sum ( $array , $max ) < //For Reference, use: "&$array"
$sum = 0 ;
for ( $i = 0 ; $i < 2 ; $i ++)#$array[$i]++; //Uncomment this line to modify the array within the function.
$sum += $array [ $i ];
>
return ( $sum );
>

$max = 1E7 //10 M data points.
$data = range ( 0 , $max , 1 );

$start = microtime ( true );
for ( $x = 0 ; $x < 100 ; $x ++)$sum = sum ( $data , $max );
>
$end = microtime ( true );
echo «Time: » .( $end — $start ). » s\n» ;

/* Run times:
# PASS BY MODIFIED? Time
— ——- ——— —-
1 value no 56 us
2 reference no 58 us

3 valuue yes 129 s
4 reference yes 66 us

1. PHP is already smart about zero-copy / copy-on-write. A function call does NOT copy the data unless it needs to; the data is
only copied on write. That’s why #1 and #2 take similar times, whereas #3 takes 2 million times longer than #4.
[You never need to use &$array to ask the compiler to do a zero-copy optimisation; it can work that out for itself.]

2. You do use &$array to tell the compiler «it is OK for the function to over-write my argument in place, I don’t need the original
any more.» This can make a huge difference to performance when we have large amounts of memory to copy.
(This is the only way it is done in C, arrays are always passed as pointers)

3. The other use of & is as a way to specify where data should be *returned*. (e.g. as used by exec() ).
(This is a C-like way of passing pointers for outputs, whereas PHP functions normally return complex types, or multiple answers
in an array)

5. Sometimes, pass by reference could be at the choice of the caller, NOT the function definitition. PHP doesn’t allow it, but it
would be meaningful for the caller to decide to pass data in as a reference. i.e. «I’m done with the variable, it’s OK to stomp
on it in memory».
*/
?>

Источник

Оцените статью