Would someone (i.e. the document maintainer or else a kind reader or two) please explain what/how the 'mixed' pseudo type is used, along with some examples? This documentation page is extremely brief and not at all clear on the subject. I've also tried looking through other code I've found and cannot seem to find anybody actually using it. -- Thanks very much.
本ドキュメントにおける疑似的な型および変数
mixed
mixed は、引数に多様な型 (全てである必要はない) を使うことができることを示します。
例えば gettype() 関数は全ての PHP の型を受け入れるのに対し、 str_replace() は文字列と配列のみを受け入れます。
callback
call_user_func() や usort() 等の関数は、ユーザが定義するコールバック関数を引数として受け入れます。 コールバック関数は、単純な関数だけでなく、オブジェクトのメソッド あるいはクラスの静的メソッドであってもかまいません。
PHP 関数はその名前を単に文字列として渡されます。 どのようなビルトインまたはユーザ定義の関数も渡すことができます。 ただし、 array(), echo(), empty(), eval(), exit(), isset(), list(), print() あるいは unset() はコールバックとしては使用できないことに注意しましょう。
オブジェクトのインスタンスを渡すための方法の 1 つは、 オブジェクトを 0 番目の要素、 メソッド名を 1 番目の要素として含む配列を渡すことです。
静的なクラスメソッドの場合、 0 番目の要素としてオブジェクトを渡す代わりにクラス名を渡すことにより、 オブジェクトのインスタンスを作成せずに渡すことができます。
一般的なユーザ定義関数とは異なり、create_function() では無名コールバック関数を作成することができます。
例1 コールバック関数の例
<?php
// コールバック関数の例
function my_callback_function() {
echo 'hello world!';
}
// コールバックメソッドの例
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}
// タイプ 1: 単純なコールバック
call_user_func('my_callback_function');
// タイプ 2: 静的クラスメソッドのコール
call_user_func(array('MyClass', 'myCallbackMethod'));
// タイプ 3: オブジェクトメソッドのコール
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
// タイプ 4: 静的クラスメソッドのコール (PHP 5.2.3 以降)
call_user_func('MyClass::myCallbackMethod');
// タイプ 5: 相対指定による静的クラスメソッドのコール (PHP 5.3.0 以降)
class A {
public static function who() {
echo "A\n";
}
}
class B extends A {
public static function who() {
echo "B\n";
}
}
call_user_func(array('B', 'parent::who')); // A
?>
注意: PHP4 では、実際のオブジェクトを指すコールバックを作成するには 参照を使用する必要があります。そのコピーを使用してはいけません。 詳細は 参照についての説明 を参照ください。
void
返り値の型が void である場合は、 返り値に意味がないことを表します。パラメータ一覧で void が使用されている場合は、 その関数がパラメータを受け付けないことを表します。
...
関数のプロトタイプ宣言における $... は、 …など を表します。 この変数名を用いるのは、たとえば任意の数の引数を取りうる関数などです。
本ドキュメントにおける疑似的な型および変数
13-Dec-2007 08:08
24-May-2007 01: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.
09-Feb-2007 06: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');
?>
01-Feb-2007 06: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
12-Aug-2005 09:17
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) {
............
}
}
?>
