如何按名称调用方法?

时间:2011-10-12 20:57:51

标签: php eval zend-search-lucene

我想将一个行对象传递给一个函数,该函数使用存储在行中的配置信息来构建Zend Search Lucene文档并将其添加到我的索引中。

存储的信息如下所示:

protected $_index = array(
    'url' => array('UnIndexed', 'getUrl()'),
    'fragment' => array('UnIndexed', 'getFragment()'),
    'edited' => array('UnIndexed', 'edited'),
    'title' => array('Text', 'getTitle()'),
    'summary' => array('Text', 'getSummary()'),
    'text' => array('UnStored', 'getText()'),
);

这是我打电话的功能:

public static function addDoc(My_Db_Table_Row_Abstract $row)
{
    $config = $row->getIndexConfig();
    if (empty($config)) {
        return;
    }

    // setup the document
    $document = new Zend_Search_Lucene_Document();
    $document->addField(Zend_Search_Lucene_Field::keyword('table_name', $row->getTable()->info('name')))
            ->addField(Zend_Search_Lucene_Field::keyword('table_row_key', $row->getIndexRowKey()));

    // add the configured fields
    foreach ($config as $name => $opts) {
        $type = $opts[0];
        $value = eval('return $row->' . $opts[1]);
        switch ($type) {
            case 'Keyword' :
                $field = Zend_Search_Lucene_Field::keyword($name, $value);
                break;
            case 'UnIndexed' :
                $field = Zend_Search_Lucene_Field::unIndexed($name, $value);
                break;
            case 'Binary' :
                $field = Zend_Search_Lucene_Field::binary($name, $value);
                break;
            case 'Text' :
                $field = Zend_Search_Lucene_Field::text($name, $value);
                break;
            case 'UnStored ' :
                $field = Zend_Search_Lucene_Field::unStored($name, $value);
                break;
        }
        $document->addField($field);
    }

    // add to the index
    self::getIndex()->addDocument($document);
}

如您所见,我需要通过函数调用将信息输入到文档字段中。我担心使用eval(),但我不知道另一种方式。有没有更好的方法来实现相同的功能?


我更改了配置数据以使用所有字段的功能(删除括号),并更低了type的第一个字母,因此我也可以将其用于call_user_func:< / p>

protected $_index = array(
    'url' => array('unIndexed', 'getUrl'),
    'fragment' => array('unIndexed', 'getFragment'),
    'edited' => array('unIndexed', 'getEdited'),
    'active_self' => array('keyword', 'isActive'),
    'active_block' => array('keyword', 'isActiveBlock'),
    'active_page' => array('keyword', 'isActiveNav'),
    'members_only' => array('keyword', 'isMembersOnly'),
    'title' => array('text', 'getTitle'),
    'summary' => array('text', 'getSummary'),
    'text' => array('unStored', 'getText'),
);

函数调用最终变得更简单了:

public static function addDoc(My_Db_Table_Row_Abstract $row)
{
    $config = $row->getIndexConfig();
    if (empty($config)) {
        return;
    }

    // setup the document
    $document = new Zend_Search_Lucene_Document();
    $document->addField(Zend_Search_Lucene_Field::keyword('table_name', $row->getTable()->info('name')))
            ->addField(Zend_Search_Lucene_Field::keyword('table_row_key', $row->getIndexRowKey()));

    // add the configured fields
    foreach ($config as $name => $opts) {
        $value = call_user_func(array($row, $opts[1]));
        $field = call_user_func(array('Zend_Search_Lucene_Field', $opts[0]), $name, $value);
        $document->addField($field);
    }

    // add to the index
    self::getIndex()->addDocument($document);
}

3 个答案:

答案 0 :(得分:2)

您也可以使用call_user_func。这是PHP doc

示例:

<?php
function barber($type)
{
    echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>

输出:

 You wanted a mushroom haircut, no problem
 You wanted a shave haircut, no problem

例如,您需要更改:

$value = eval('return $row->' . $opts[1]);

通过

$value = call_user_func($row->' . $opts[1]);

答案 1 :(得分:2)

我想你可以做到:

// ...
$type = $opts[0];
$method = $opts[1];
$value = $row->$method;

或者:

// ...
$type = $opts[0];
call_user_func(array($row, $opts[1]));

答案 2 :(得分:2)

从方法名称中删除parens:

protected $_index = array(
    'url' => array('UnIndexed', 'getUrl'),
    'fragment' => array('UnIndexed', 'getFragment'),
    ...

替换此属性:

'edited' => array('UnIndexed', 'edited'),

使用等效的getter方法:

'edited' => array('UnIndexed', 'getEdited'),

然后就这样做:

$method = $opts[1];
$value = $row->$method();
相关问题