<?php
class abc{
function func($argument) {
$argument="It works";
}
}
$obj=new abc;
$argument_to_be_changed="No it doesn't work";
call_user_method("func", $obj, &$argument_to_be_changed);
echo "Result : ".$argument_to_be_changed;
?>
This code is working. But will through some warning message which you can hide by configuring php.ini
call_user_method
(PHP 4, PHP 5)
call_user_method — Wywołuje metodę użytkownika na zadanym obiekcie [przestarzałe]
Opis
mixed call_user_method
( string $nazwa_metody
, object &$>obiekt
[, mixed $parametr
[, mixed $...
]] )
Ostrzeżenie
Funkcja call_user_method() jest przestarzała od PHP 4.1.0, zamiast niej użyj funkcji call_user_func() posługując się składnią array(&$obiekt, "nazwa_metody").
Wywołuje metodę nazwaną method_name z obiektu obiekt zdefiniowanego przez użytkownika. Przykład użycia znajdziesz niżej, gdzie definiujemy klasę, tworzymy jej egzemplarz i używamy funkcji call_user_method() by pośrednio wywołać jej metodę drukuj_info.
<?php
class Kraj {
var $NAZWA;
var $TLD;
function Kraj($nazwa, $tld) {
$this->NAZWA = $nazwa;
$this->TLD = $tld;
}
function drukuj_info($przedc = "") {
echo $przedc."Kraj: ".$this->NAZWA."\n";
echo $przedc."Domena TLD: ".$this->TLD."\n";
}
}
$kraj = new Kraj("Peru", "pe");
echo "* Wywołuję metodę bezpośrednio na obiekcie\n";
$kraj->drukuj_info();
echo "\n* Wywołuję tę samą metodę pośrednio\n";
call_user_method ("drukuj_info", $kraj, "\t");
?>
Patrz także: call_user_func_array() i call_user_func().
call_user_method
ravichandran_11 at yahoo dot co dot in
10-Mar-2008 01:32
10-Mar-2008 01:32
j dot h at h-elektro dot de
05-Feb-2007 09:11
05-Feb-2007 09: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
paulo at emd dot com dot br
18-Sep-2000 02:12
18-Sep-2000 02: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 02:04
21-Aug-2000 02: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
