0

I'm making an expensive soap call and would like to cache it even if it is empty through the duration of the script and page load. It's entirely possible that in the third party database the object is empty and null. I don't want to make five soap calls if the object is empty. I feel like using PHP empty() and isset() this way is hackish. It works but there is a lot of funky logic involved. Is there a better way?

I need the drupal_static so if the $third_party_info_object is change anywhere else all the caches can get cleaned.

function my_module_get_information($user) {

  third_party_soap_initialize();

  $third_party_info = &drupal_static(__FUNCTION__);

  if (!isset($third_party_info) && is_null($third_party_info)) {
    // work some magic here
    $account_object = new thirdPartyAccounObject();
    $account_object->setThirdParyAccountId($user->uid);

    $third_party_info_object = new ThirdPartyInfo();
    $response = $third_party_info_object->getByAccount($account_object, false);

    $returnCode = {get the $returnCode here}

    if ($returnCode == 200) {
      $third_party_info = $response->getThirdPartyInfo();
    }
  }

  if (!isset($third_party_info)) {
    $third_party_info = false;
  }

  return $third_party_info;
}
1

drupal_static()'s default value is null. So instead of doing this

<?php
$third_party_info = &drupal_static(__FUNCTION__);
if (!isset($third_party_info) && is_null($third_party_info)) {
?>

Try this

<?php
$third_party_info = &drupal_static(__FUNCTION__);
if (is_null($third_party_info)) {
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.