PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Type Juggling> <NULL
Last updated: Sat, 17 Jul 2004

view this page in

Pseudo-types 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

Some functions like call_user_func() or usort() accept user defined callback functions as a parameter. Callback functions can not only be simple functions but also object methods including static class methods.

A PHP function is simply passed by its name as a string. You can pass any builtin or user defined function with the exception of array(), echo(), empty(), eval(), exit(), isset(), list(), print() and unset().

A method of an instantiated object is passed as an array containing an object as the element with index 0 and a method name as the element with index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object as the element with index 0.

Örnek 6-11. Callback function examples

<?php

// simple callback example
function my_callback_function() {
    echo
'hello world!';
}
call_user_func('my_callback_function');

// method callback examples
class MyClass {
    function
myCallbackMethod() {
        echo
'Hello World!';
    }
}

// static class method call without instantiating an object
call_user_func(array('MyClass', 'myCallbackMethod'));

// object method call
$obj = new MyClass();
call_user_func(array(&$obj, 'myCallbackMethod'));
?>



Type Juggling> <NULL
Last updated: Sat, 17 Jul 2004
 
add a note add a note User Contributed Notes
Pseudo-types used in this documentation
Hayley Watson
23-May-2007 10:44
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.
levi at alliancesoftware dot com dot au
08-Feb-2007 02:44
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');
?>
Edward
01-Feb-2007 02:15
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

Type Juggling> <NULL
Last updated: Sat, 17 Jul 2004
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites