如何通过名称调用方法? - php

我想将行对象传递给一个函数,该函数使用存储在行中的配置信息来构建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

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);
}

参考方案

您也可以使用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]);

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

php:是否有充分的理由引用所有数组键/索引? - php

我正在遍历别人的代码,他们总是避免转义其数组键。例如:$ row_rsCatalogsItems [名称]代替$ row_rsCatalogsItems ['名称']因此,我不断地对自己接触的所有事物进行微小的更改,以应对这些惰性。但是现在我想知道这样做是否有很多好处。我得到它会在默认为字符串之前检查常量(我在处理常量时会讨厌php中的行为,因为即使未定义,…

phpWord中的粗体,空格和缩进文本 - php

我有一些要加粗的文本,与前几段和后几段分开并缩进。我不能让所有三个属性一起工作。这适用于粗体和空格:$section = $phpWord->addSection(); $section->addText( 'Re: Your Application for Post of Security Guard', array(�…

PHP数组可以这样做吗? - php

可以说;我有一个$ friends数组,其中有2,000个不同的friendID号+我有一个带有10,000 bulletinID号的$ bulletins数组,该$ bulletins数组还将具有另一个值,该ID的用户ID是发布公告条目的用户现在可以获取所有具有与FriendsID数组中的userID匹配的userID的bulletinID号吗?甚至有可能…

哪个更好的做法?从Jquery响应获取HTML - php

这只是一个问题,以了解人们如何以及如何做到这一点,但是假设用户向列表中添加了一些内容,完成后,它将运行下面的ajax并更新.user-stream-list$.ajax({ url: "user-stream-list.php", success: function(data){ $(".user-stream-list…