如何获取php方法的变量

如何获取php方法的变量,php,oop,breadcrumbs,Php,Oop,Breadcrumbs,我想创建自动面包屑,因为数组包含带有2个键(链接、标题)的子数组。 像这样: array ( 0 => array ( 'link' => 'index', 'title' => 'Home :)', ), 1 => array ( 'link' => 'advanced', 'title' => 'Advanced Setting :)' ) ); 笔记 当我

我想创建自动面包屑,因为数组包含带有2个键(链接、标题)的子数组。 像这样:

array (
    0 => array (
        'link' => 'index',
        'title' => 'Home :)',
    ),
    1 => array (
        'link' => 'advanced',
        'title' =>  'Advanced Setting :)'
    )
);
笔记 当我使用下面的代码时,它会给出这个结果,但我无法获得变量
$mytitle
上显示的标题

class Setting extends Controller {
    public function __construct() {
        $relflection = new ReflectionClass ( get_class ( $this ) );
        $methods = $relflection->getMethods ( ReflectionMethod::IS_PUBLIC );
        if (is_array ( $methods )) {
            foreach ( $methods as $method ) {
                if ($method->class == get_class ( $this )) {
                    $breadcrumb[] = array("link" => $method->name, "title" => "???");
                }
            }
        }
        $breadcrumb = $breadcrumb;
    }

    public function index() {
        $mytitle = "Home :)";
    }

    public function advanced() {
        $mytitle = "Advanced Setting :)";
    }
}

这里有人能给我一个正确的解决方案吗?

我希望我离您的需求不会太远,但这可以奏效:

首先将面包屑分配给实际属性,否则它将无法在构造函数的作用域之外访问:

$this->breadcrumb = $breadcrumb;
然后,根据问题,您可以设计一种获得函数名的方法:

function getMyCallerName() {
  $array = debug_backtrace();
  return $array[1]['function'];
} 
// since you call a function to have your function name, the right caller will be the second function in the backtrace
您需要一个函数从面包屑中获取所需的标题:

function getTitleFromLink($link) {
  foreach ($this->breadcrumb as $array) {
  if ($array['link'] == $link) return $array['title'];
  }
}
然后您可以在
index()函数中使用它:

public function index() {
  $title = getTitleFromLink(getMyCallerName()); // should be "Home :)"
}
public function advanced() {
  $title = getTitleFromLink(getMyCallerName()); // should be "Advanced Setting :)"
}
然后可以在索引函数中添加任何逻辑


请确保将这些函数放入类中,否则
getTitleFromLink()
将无法使用
breadcrumb
属性调用索引()并返回该函数中的标题该方法将具有功能并返回其他内容。
“title”=>$relflection->getMethod($method->name)->invoke()