PHP: call_user_method - Manual

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

search for in the

class_exists call_user_method_array

Last updated: Sat, 27 Jan 2007

view this page in

# call_user_method

(PHP 3 >= 3.0.3, PHP 4, PHP 5)

call_user_method -- Вызывает метод указанного объекта [устаревшее]

## Описание

mixed call_user_method ( string method_name, object obj [, mixed parameter [, mixed ...]] )

Внимание
Функция call_user_method_array() устарела с выходом 4.1.0: используйте функцию call_user_func() с `array(&$obj, "method_name")` в качестве параметра имени функции.

Вызываем метод `method_name` объекта `obj`. Ниже приведен пример использования функции, в которой мы определяем класс, создаём его объект и используем call_user_method() для вызова метода `print_info`.

`<?php
class Country {
var $NAME;
var $TLD;
function Country($name, $tld)
{
$this->NAME = $name;
$this->TLD = $tld;
}
function print_info($prestr = "")
{
echo $prestr . "Country: " . $this->NAME . "\n";
echo $prestr . "Top Level Domain: " . $this->TLD . "\n";
}
}
$cntry = new Country("Peru", "pe");
echo "* Calling the object method directly\n";
$cntry->print_info();
echo "\n* Calling the same method indirectly\n";
call_user_method("print_info", $cntry, "\t");
?>`

См. также call_user_func_array() и call_user_func().

add a note User Contributed Notes
call_user_method
j dot h at h-elektro dot de
05-Feb-2007 12:11
`It does not work to use Pointers as Arguments:
<?php
class abc{
function func(&$argument) {
$argument="It works";
}
}
$obj=new abc;
$argument_to_be_changed="No it doesnt";
call_user_method("func", $obj, $argument_to_be_changed);
echo "Result".$argument_to_be_changed;
?>
The result is: "No it doesnt".
Regards
der Jan`
jonathanMAPS_ON at NO_SPAMsharpmedia dot net
13-Mar-2001 03:06
`You can also dynamically call functions through variable variables...for example:
<?php
class xyz{
function mybar($str)
{
echo $str;
}
}
$xyz = new xyz;
$foo = 'bar';
$xyz->{'my'.$foo}('dynamic call to function');
?>`
paulo at emd dot com dot br
18-Sep-2000 05:12
`This function is very similar to this:
$method="Print";
$object->$method($param1,$param2);
Note the extra $ after the ->`
jmcastagnetto at php dot net
21-Aug-2000 05:04
`You can pass a variable number of parameters to a function, use a definition like:
function mymethod ($v1, $v2, $v3="", $v4="")
and then you can pass 2, 3 or 4 parameters. This is explained in the "Functions" section of the manual.
See also the PHP4 functions: func_num_args(), func_get_arg(), and func_get_args(), and examples therein`
add a note
class_exists call_user_method_array

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST