Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:45:45+00:00 2026-05-26T06:45:45+00:00

I want to pass a row object to a function that uses the configuration

  • 0

I want to pass a row object to a function that uses the configuration information stored in the row to build a Zend Search Lucene Document and add it to my index.

The information stored looks like this:

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()'),
);

Here’s the function I call:

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

As you can see, I need to get information via function calls into the document fields. I am concerned about using eval(), but I don’t know another way. Is there a better way to accomplish the same functionality?


SOLUTION

I changed the configuration data to use functions for all the fields (removing the parenthesis), and lower-cased the first letter of the type so I could use that for call_user_func as well:

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'),
);

And the function call, which ended up being much simpler:

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:45:46+00:00Added an answer on May 26, 2026 at 6:45 am

    You can also use call_user_func. Here is the PHP doc.

    Example:

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

    Output:

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

    For you example, you need to change :

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

    by

    $value = call_user_func($row->' . $opts[1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass an integer value to a form in .Net so that
I want to pass in the tType of a class to a function, and
I want to build a ApplicationSetting for my application. The ApplicationSetting can be stored
i want to know how to pass particular word or character in search query
I want to coerce a Str into a DBIx::Class::Row object for an attribute in
I want to pass an int list (List) as a declarative property to a
I want to pass an enum value as command parameter in WPF, using something
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to pass a variable to a UIButton action, for example NSString *string=@one;
I want to pass a byte[] to a method takes a IntPtr Parameter in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.