参考PHP类函数方法

blu*_*dtt 3 php

我有一节课:

class demo {

      function newDemo(){
          $v=$this->checkDemo;
          $v('hello'); // not working this reference, or how to do this?
      }

      function checkDemo($a){
          ...
          return $a;
      }
           }
Run Code Online (Sandbox Code Playgroud)

那么,我如何在类中引用checkDemo函数方法呢?

Nie*_*sol 6

要使对象方法可调用,您需要一个数组.索引0是实例,索引1是方法的名称:

$v = Array($this,"checkDemo");
$v("hello");
Run Code Online (Sandbox Code Playgroud)

编辑:请注意,此功能仅适用于PHP 5.4


Tom*_*ers 5

你像这样分配它:

$v = 'checkDemo';
$this->$v('hello');
Run Code Online (Sandbox Code Playgroud)

查看文档以获取更多示例。

虽然我不完全确定你为什么要这样做,但就是这样。