Php class parameter array

Php class parameter array

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)

Читайте также:  Document

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».
*/
?>

Источник

Php class parameter array

While waiting for native support for typed arrays, here are a couple of alternative ways to ensure strong typing of arrays by abusing variadic functions. The performance of these methods is a mystery to the writer and so the responsibility of benchmarking them falls unto the reader.

PHP 5.6 added the splat operator (. ) which is used to unpack arrays to be used as function arguments. PHP 7.0 added scalar type hints. Latter versions of PHP have further improved the type system. With these additions and improvements, it is possible to have a decent support for typed arrays.

function typeArrayNullInt (? int . $arg ): void >

function doSomething (array $ints ): void (function (? int . $arg ) <>)(. $ints );
// Alternatively,
( fn (? int . $arg ) => $arg )(. $ints );
// Or to avoid cluttering memory with too many closures
typeArrayNullInt (. $ints );

function doSomethingElse (? int . $ints ): void /* . */
>

$ints = [ 1 , 2 , 3 , 4 , null ];
doSomething ( $ints );
doSomethingElse (. $ints );
?>

Both methods work with all type declarations. The key idea here is to have the functions throw a runtime error if they encounter a typing violation. The typing method used in doSomethingElse is cleaner of the two but it disallows having any other parameters after the variadic parameter. It also requires the call site to be aware of this typing implementation and unpack the array. The method used in doSomething is messier but it does not require the call site to be aware of the typing method as the unpacking is performed within the function. It is also less ambiguous as the doSomethingElse would also accept n individual parameters where as doSomething only accepts an array. doSomething’s method is also easier to strip away if native typed array support is ever added to PHP. Both of these methods only work for input parameters. An array return value type check would need to take place at the call site.

If strict_types is not enabled, it may be desirable to return the coerced scalar values from the type check function (e.g. floats and strings become integers) to ensure proper typing.

same data type and same value but first function declare as a argument type declaration and return int(7)
and second fucntion declare as a return type declaration but return int(8).

function argument_type_declaration(int $a, int $b) return $a+$b;
>

function return_type_declaration($a,$b) :int return $a+$b;
>

Источник

Functions with Object and Array arguments

— Data from the function parameters can be of different types: number, string, array, or even object. To check the parameters data type, you can use PHP specific functions, like: is_string(), is_array(), is_object() , etc.

There is also another option for objects and arrays, by specifying in the same declaration the parameter and the allowed data type.

function function_name(ClassName $parameter) < // function body >
function function_name(array $parameter, array $parameter2) < // function body >

— These formulas verify and ensure that the $parameter data to be an object instance of a certain class, specified in ClassName , or, at the other syntax, an Array.

— Example :
In the next example we set a class, named AClas, a function ( fObj() ) which only accepts an argument that is an object instance of the AClas, and another function ( fArr() ) with two parameters, the first argument must be an array.

 > // this function accepts only an argument that is an object instance of the AClas function fObj(AClas $objPar) < echo $objPar->aMet(). '
'; // calls the aMet() method > // the first argument passed to this function must be an Array function fArr(array $arrPar1, $par2) < print_r($arrPar1); echo '
'. $par2; > // object instance of the AClas $obj = new AClas(); fObj($obj); // calls the fObj() with $obj argument $aray = array('php', 'mysql', 'javascript'); $str = 'https://coursesweb.net'; fArr($aray, $str); // calls the fArr() function ?>

As you can notice, the special defined parameters can be used togheter with normal parameter ( fArr(array $arrPar1, $par2) )
The code above will output:

If one of the fObj() or fArr functions is not called with the data type required in its definition, will cause an error. For example, if you call the fArr() function, and the first argument isn’t an array (i.g fArr(8, $str); ), the script will return an error like this:

Class methods with object and array arguments

If your class has methods which use object or array arguments, you can specify in the method definition the type of the allowed arguments.
— Example :
In the following example we define two classes (AClas and BClas). The BClas has two methods: bObj() is defined to accepts for its argument only instance of the AClas, and bArr() only accepts data type Array.

 > // Define BClas class class BClas < // only uses object instance of AClas public function bObj(AClas $obj_par) < return $obj_par->aMet(); // cxalls the meethod aMet() of the AClas > // only accepts an array argument public function bArr(array $arr_par) < print_r($arr_par); >> // object instance of the AClas $objAC = new AClas(); // object instance of the BClas $objBC = new BClas(); // calls the methods of the BClas, with the correct arguments (AClas instance for bObj(), and an array for bArr() ) echo $objBC->bObj($objAC). '
'; echo $objBC->bArr( array('courses', 'tutorials') ); ?>

Источник

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