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

search for in the

内部(ビルトイン)関数> <返り値
Last updated: Fri, 15 Aug 2008

view this page in

可変関数

PHP は可変関数(variable functions)の概念をサポートします。 これにより、変数名の後に括弧が付いている場合、その値が何であろうと PHPは、同名の関数を探し実行を試みます。 この機能は、コールバック、関数テーブル等を実装するために使用可能です。

可変関数は、echo(), unset(), isset(), empty(), include(), print() のような言語構造と組み合わせて使用する ことはできません。これらの言語構造を可変変数として使うには 独自のラッパ関数を使う必要があります。

例1 可変関数の例

<?php
function foo()
{
    echo 
"In foo()<br />\n";
}

function 
bar($arg '')
{
    echo 
"In bar(); argument was '$arg'.<br />\n";
}

// This is a wrapper function around echo
function echoit($string)
{
    echo 
$string;
}

$func 'foo';
$func();        // This calls foo()

$func 'bar';
$func('test');  // This calls bar()

$func 'echoit';
$func('test');  // This calls echoit()
?>

オブジェクトのメソッドを可変関数を使ってコールすることもできます。

例2 可変メソッドの例

<?php
class Foo
{
    function 
Variable()
    {
        
$name 'Bar';
        
$this->$name(); // Bar() メソッドのコール
    
}
    
    function 
Bar()
    {
        echo 
"This is Bar";
    }
}

$foo = new Foo();
$funcname "Variable";
$foo->$funcname();  // $foo->Variable() をコールする

?>

可変変数function_exists()も参照してください。



内部(ビルトイン)関数> <返り値
Last updated: Fri, 15 Aug 2008
 
add a note add a note User Contributed Notes
可変関数
sandovalpaolo at gmail dot com
17-Apr-2008 09:19
//function to connect to a database

function connect($localhost, $root, $password, $ddbb)
{
$host = $localhost;
$user = $root;
$code = $password;
$dbname = $ddbb;

if (!$net = mysql_connect($host, $user, $code)) {
    echo "could't connect to $host";
    exit;
}

mysql_select_db($dbname);

}

// call de function first example
connect("localhost", "user", "code", $page);
// second example
connect("localhost", "user","","");
mike at mike-griffiths dot co dot uk
27-Jun-2007 10:37
It may be useful to note that 'variable functions' can only be used in conjunction with functions, and not language constructs such as echo, die, exit, etc.
boards at gmail dot com
23-Jan-2006 02:07
If you want to call a static function (PHP5) in a variable method:

Make an array of two entries where the 0th entry is the name of the class to be invoked ('self' and 'parent' work as well) and the 1st entry is the name of the function.  Basically, a 'callback' variable is either a string (the name of the function) or an array (0 => 'className', 1 => 'functionName').

Then, to call that function, you can use either call_user_func() or call_user_func_array().  Examples:

<?php
class A {

  protected
$a;
  protected
$c;

  function
__construct() {
   
$this->a = array('self', 'a');
   
$this->c = array('self', 'c');
  }

  static function
a($name, &$value) {
    echo
$name,' => ',$value++,"\n";
  }

  function
b($name, &$value) {
   
call_user_func_array($this->a, array($name, &$value));
  }

  static function
c($str) {
    echo
$str,"\n";
  }

  function
d() {
   
call_user_func_array($this->c, func_get_args());
  }

  function
e() {
   
call_user_func($this->c, func_get_arg(0));
  }

}

class
B extends A {

  function
__construct() {
   
$this->a = array('parent', 'a');
   
$this->c = array('self', 'c');
  }

  static function
c() {
   
print_r(func_get_args());
  }

  function
d() {
   
call_user_func_array($this->c, func_get_args());
  }

  function
e() {
   
call_user_func($this->c, func_get_args());
  }

}

$a =& new A;
$b =& new B;
$i = 0;

A::a('index', $i);
$a->b('index', $i);

$a->c('string');
$a->d('string');
$a->e('string');

# etc.
?>
Storm
03-May-2005 11:34
This can quite useful for a dynamic database class:

(Note: This just a simplified section)

<?php
class db {

    private
$host = 'localhost';
    private
$user = 'username';
    private
$pass = 'password';
    private
$type = 'mysqli';
   
    public
$lid = 0;

   
// Connection function
   
function connect() {
       
$connect = $this->type.'_connect';
           
        if (!
$this->lid = $connect($this->host, $this->user, $this->pass)) {
            die(
'Unable to connect.');
        }
 }
}
$db  = new db;
$db->connect();
?>

Much easier than having multiple database classes or even extending a base class.
ian at NO_SPAM dot verteron dot net
20-Dec-2002 11:33
A good method to pass around variables containing function names within some class is to use the same method as the developers use in preg_replace_callback - with arrays containing an instance of the class and the function name itself.

function call_within_an_object($fun)
{
  if(is_array($fun))
  {
    /* call a function within an object */
    $fun[0]->{$fun[1]}();
  }
  else
  {
    /* call some other function */
    $fun();
  }
}

function some_other_fun()
{
  /* code */
}

class x
{
  function fun($value)
  {
    /* some code */
  }
}

$x = new x();

/* the following line calls $x->fun() */
call_within_an_object(Array($x, 'fun'));

/* the following line calls some_other_fun() */
call_within_an_object('some_other_fun');
madeinlisboa at yahoo dot com
05-Sep-2002 08:14
Finally, a very easy way to call a variable method in a class:

Example of a class:

class Print() {
    var $mPrintFunction;

    function Print($where_to) {
        $this->mPrintFunction = "PrintTo$where_to";
    }

    function PrintToScreen($content) {
        echo $content;
    }

    function PrintToFile($content) {
        fputs ($file, $contents);
    }

.. .. ..

    // first, function name is parsed, then function is called
    $this->{$this->mPrintFunction}("something to print");
}
msmith at pmcc dot com
03-May-2002 07:49
Try the call_user_func() function.  I find it's a bit simpler to implement, and at very least makes your code a bit more readable... much more readable and simpler to research for someone who isn't familiar with this construct.
anpatel at NOSPAM_cbu dot edu
18-Mar-2002 01:11
Yes interpolation can be very tricky. I suggest that you always use parenthesis, or curly brackets(whichever applies) to make your expression clear.

Dont ever depend on a language's expression parse preference order.
retro at enx dot org
14-Jan-2002 11:18
Another way to have php parse a variable within an object as a function is to simply set a temporary variable to its value. For example:

$obj->myfunction = "foo";
$x = $obj->myfunction;
$x(); // calls the function named "foo"

内部(ビルトイン)関数> <返り値
Last updated: Fri, 15 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites