PHP 7.0.6 Released

Pseudo-types and variables used in this documentation

mixed

mixed indicates that a parameter may accept multiple (but not necessarily all) types.

gettype() for example will accept all PHP types, while str_replace() will accept strings and arrays.

number

number indicates that a parameter can be either integer or float.

callback

callback pseudo-types was used in this documentation before callable type hint was introduced by PHP 5.4. It means exactly the same.

array|object

array|object indicates that a parameter can be either array or object.

void

void as a return type means that the return value is useless. void in a parameter list means that the function doesn't accept any parameters.

...

$... in function prototypes means and so on. This variable name is used when a function can take an endless number of arguments.

User Contributed Notes

gried at NOSPAM dot nsys dot by
3 months ago
In response to phpguy at lifetoward dot com.

I tested on PHP 5.5.9 and found that his claims are outdated.
Code below can be used to check it.

PS: It's probably best to downvote that comment, so people who are new to PHP won't be misinformed.

<?php
error_reporting
(E_ALL);

$test = [2, 1, 5, 6, 3, 0];
echo
var_dump($test).'<hr>';

$reverse = 0;
usort($test, ($reverse?'sortRev':'sortForw') );
echo
var_dump($test).'<hr>';

function
sortForw($a, $b) {
  return
$a - $b;
}

function
sortRev($a, $b) {
  return
$b - $a;
}
phpguy at lifetoward dot com
6 years ago
I noticed two important thing about putting callbacks into an arg list when calling a function:

1. The function to which the callback refers must be defined earlier in the source stream. So for example:

function main() {...; usort($array, 'sortfunction'); ... }
function sortfunction($a, $b){ return 0; }

Will NOT work, but this will:

function sortfunction($a, $b){ return 0; }
function main() {...; usort($array, 'sortfunction'); ... }

2. It's not really just a string. For example, this doesn't work:

usort($array, ($reverse?'reversesorter':'forwardsorter'));

I found these two discoveries quite counterintuitive.
michael dot martinek at gmail dot com
6 years ago
The documentation is a little confusing, and with the recent OO changes it adds a little more to the confusion.

I was curious whether you could pass an object through the user func, modify it in that callback and have the actual object updated or whether some cloning was going on behind the scenes.

<?php
   
class Test
   
{
        var
$sValue = 'abc';

        function
testing($objTest)
        {
           
$objTest->sValue = '123';
        }
    }

   
$obj = new Test();

   
call_user_func(array($obj, 'testing'), $obj);

   
var_dump($obj);

?>

This works as expected: The object is not cloned, and $sValue is properly set to '123'. With the OO changes in PHP 5, you don't need to do "function testing(&$objTest)" as it is already passed by reference.
Hayley Watson
8 years ago
The mixed pseudotype is explained as meaning "multiple but not necessarily all" types, and the example of str_replace(mixed, mixed, mixed) is given where "mixed" means "string or array".
Keep in mind that this refers to the types of the function's arguments _after_ any type juggling.
sahid dot ferdjaoui at gmail dot com
7 years ago
An example with PHP 5.3 and lambda functions

<?php

  array_map
(function ($value) {
    return new
MyFormElement ($value);
  },
$_POST);

?>
Edward
9 years ago
To recap mr dot lilov at gmail dot com's comment: If you want to pass a function as an argument to another function, for example "array_map", do this:

regular functions:
<?
array_map(intval, $array)
?>

static functions in a class:
<?
array_map(array('MyClass', 'MyFunction'), $array)
?>

functions from an object:
<?
array_map(array($this, 'MyFunction'), $array)
?>

I hope this clarifies things a little bit
liam at helios-sites dot com
5 years ago
Note that (e.g.) usort calls on static methods of classes in a namespace need to be laid out as follows:

usort($arr, array('\Namespace\ClassName', 'functionName'));
mike@EastGhostCom
3 years ago
If you pass a string as the callback function (i.e., 2nd parm to preg_replace_callback()), then PHP will interpret it as a function's name in the current scope -- and Main::dada_cb is not a valid function name in any scope.

If you want to specify a static method of a class as the callback (i.e., "Main::dada_cb"), then you must pass as 2nd parm to preg_replace_callback:

array( 'Main', 'dada_cb')

And, if you want to use as a callback some method of an instantiated object (i.e., $object->dada_cb), then you must pass as the 2nd parm to preg_replace_callback:

array( $object, 'dada_cb' )
levi at alliancesoftware dot com dot au
9 years ago
Parent methods for callbacks should be called 'parent::method', so if you wish to call a non-static parent method via a callback, you should use a callback of
<?
// always works
$callback = array($this, 'parent::method')

// works but gives an error in PHP5 with E_STRICT if the parent method is not static
$callback array('parent', 'method');
?>
To Top