Accessing Drupal core functions in object-oriented code: Call to undefined function I'm a Drupal procedural programmer new to OOP and enjoying it, so some of the techniques are new to me.

I'm getting a fatal error when I'm calling a standard drupal function from within code called within an object that creates a custom Rule. It appears that it's looking for the function drupal_json_encode_helper on the Action object rather than from where it's supposed to look. But why? And how to I tell it to search within Drupal-land rather than object-land?

I'm on D7 with xautoload and composer, modifying the fluxdropbox module that uses fluxservices to use PSR-0 for external libraries.


  "Fatal error: Call to undefined function
  Drupal\fluxdropbox\Plugin\Rules\Action\drupal_json_encode_helper()"


Here's the code: 

&lt;?php

/**
 * @file
 * Contains MakeNodes.
 */

namespace Drupal\fluxdropbox\Plugin\Rules\Action;

use Drupal\fluxdropbox\Plugin\Service\DropboxAccountInterface;
use Drupal\fluxdropbox\Rules\RulesPluginHandlerBase;

/**
 * Action for posting a status message on a page.
 */
class MakeNodes extends RulesPluginHandlerBase implements \RulesActionHandlerInterface {

  /**
   * Defines the action.
   */
  public static function getInfo() {
    return static::getInfoDefaults() + array(
      'name' =&gt; 'fluxdropbox_make_nodes',
      'label' =&gt; t('Make Nodes from Text Fiels'),
      'parameter' =&gt; array(
        'account' =&gt; static::getAccountParameterInfo(),
      ),
    );
  }

  /**
   * Executes the action.
   */
  public function execute(DropboxAccountInterface $account) {
            //here I build a nice array
            file_put_contents(drupal_json_encode_helper($feed_array), $path . '/novel.txt');

  }
}
It happens because drupal_json_encode() is defined in includes/json-encode.inc, which is not a file that is normally included when Drupal boots. To use that function, you should use code similar to the one drupal_json_encode() uses, although you really should use drupal_json_encode().

include_once DRUPAL_ROOT . '/includes/json-encode.inc';
file_put_contents(drupal_json_encode_helper($feed_array), $path . '/novel.txt');


The code I would use is the following one.

file_put_contents(drupal_json_encode($feed_array), $path . '/novel.txt');