Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Language Reference : Types : Pseudo-types and variables used in this documentation

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 simply passed by its name as a string. You can pass any built-in 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.

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

Example 2.13.  Callback function examples

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

// An example callback method
class MyClass {
   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'));
?>


Note:

In PHP4, you will have 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 in return type means that the return value is useless. void in parameters 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.

Code Examples / Notes » language.pseudo_types

edward

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


mr dot lilov

This's a useful example about callback, Look at the session_set_save_handler function.
From: http://www.zend.com/zend/spotlight/code-gallery-wade8.php
<?php
/* Create new object of class */
$ses_class = new session();
/* Change the save_handler to use the class functions */
session_set_save_handler (array(&$ses_class, '_open'),
                         array(&$ses_class, '_close'),
                         array(&$ses_class, '_read'),
                         array(&$ses_class, '_write'),
                         array(&$ses_class, '_destroy'),
                         array(&$ses_class, '_gc'));
/* Start the session */
session_start();
class session
{
   /* Define the mysql table you wish to use with
      this class, this table MUST exist. */
   var $ses_table = "sessions";
   /* Change to 'Y' if you want to connect to a db in
      the _open function */
   var $db_con = "Y";
   /* Configure the info to connect to MySQL, only required
      if $db_con is set to 'Y' */
   var $db_host = "localhost";
   var $db_user = "username";
   var $db_pass = "password";
   var $db_dbase = "dbname";
   /* Create a connection to a database */
   function db_connect() {
   ............
   }
   /* Open session, if you have your own db connection
      code, put it in here! */
   function _open($path, $name) {
   .............
   }
   /* Close session */
   function _close() {
   ..............
   }
   /* Read session data from database */
   function _read($ses_id) {
   .................
   }
   /* Write new data to database */
   function _write($ses_id, $data) {
   ...........
   }
   /* Garbage collection, deletes old sessions */
   function _gc($life) {
   ............
   }
}
?>


hayley watson

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

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');
?>


Change Language


Follow Navioo On Twitter
Introduction
Booleans
Integers
Floating point numbers
Strings
Arrays
Objects
Resource
NULL
Pseudo-types and variables used in this documentation
Type Juggling
eXTReMe Tracker