PHP Doku:: Pseudo-types and variables used in this documentation - language.pseudo-types.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzTypenPseudo-types and variables used in this documentation

Ein Service von Reinhard Neidl - Webprogrammierung.

Typen

<<NULL

Type Juggling>>

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

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 passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo(), empty(), eval(), exit(), isset(), list(), print() or unset().

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at 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 at index 0.

Apart from common user-defined function, create_function() can also be used to create an anonymous callback function.

Beispiel #1 Callback function examples

<?php 

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

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

// Type 1: Simple callback
call_user_func('my_callback_function'); 

// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod')); 

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class {
    public static function 
who() {
        echo 
"A\n";
    }
}

class 
extends {
    public static function 
who() {
        echo 
"B\n";
    }
}

call_user_func(array('B''parent::who')); // A
?>

Hinweis: In PHP4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.

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.


7 BenutzerBeiträge:
- Beiträge aktualisieren...
liam at helios-sites dot com
6.12.2010 13:44
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'));
michael dot martinek at gmail dot com
29.08.2009 18:20
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.
phpguy at lifetoward dot com
12.06.2009 2:44
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.
sahid dot ferdjaoui at gmail dot com
20.04.2009 12:19
An example with PHP 5.3 and lambda functions

<?php

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

?>
Hayley Watson
24.05.2007 7: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
8.02.2007 23: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
1.02.2007 11: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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)