Namespaced functions: How to call a variable inside a custom page template?

I’ve added some custom functions, all namespaced, to my functions.php.

I’m trying to access variables from these functions in a page template, but am getting “undefined variable” errors when the page loads.

At the top of my template file, I’m defining a variable, included below, which isn’t returning any errors:

$errors = Roots\Sage\Extras\get_job_form_errors($postdata, $files);

But halfway down the template file, I’m trying to access the $errors variable, like below, which is returning the “undefined variable” error:

if($errors['fullname']) {}

I’m relatively new to working with namespaced functions in PHP, so I have a feeling I’m missing a key step when trying to work with this variable. What am I missing?

Post your template code? Sounds like a scoping issue.

You could be setting $errors inside of a conditional statement. You could be trying to access it inside of a closure without a use declaration. You might have inadvertently unset() it somewhere.

Few ways you could be running into that problem. If you can’t figure it out, feel free to post your code so someone can actually take a look at it.

Sure, I’ve included the relevant part of my template below:

  <?php
      $jobs = Roots\Sage\Extras\get_jobs();
      $is_post = False;
      $form_submitted = False;
      if ($_SERVER['REQUEST_METHOD'] === 'POST') {
          $is_post = True;

          $postdata = $_POST;
          $files = $_FILES;
          parse_str($_SERVER['QUERY_STRING']);

          $errors = Roots\Sage\Extras\get_job_form_errors($postdata, $files);

          if (sizeof($errors) === 0) {
              Roots\Sage\Extras\submit_to_lever($jobid, $postdata, $files);
              $form_submitted = True;
          }
      }

      if( is_page('openings') ) {
          if($errors['fullname']) {
              echo '<div class="row"><div class="alert alert-danger col-md-8 col-md-offset-2 text-center">Please fill out all required fields.</div></div>';
          }
          if($errors['email']) {
              echo '<div class="row"><div class="alert alert-danger col-md-8 col-md-offset-2 text-center">Please enter a valid email address.</div></div>';
          }
      }
      if($errors['resume']) {
          echo '<div class="row"><div class="alert alert-danger col-md-8 col-md-offset-2 text-center">Please upload a resume.</div></div>';
      }
  ?>

Yeah, you’re setting it inside of a conditional statement. And you should be using isset() to check for those keys (or use the aptly named array_has_key()).

To fix…

  1. Set $errors up at the top.
$errors = [];
  1. Use isset()

Change if($errors[...]) to if(isset($errors[...]) (where ... represents the various keys you have, such as name or whatever, I just didn’t feel like typing them all out).

Has nothing to do with namespacing, by the way.

Thank you, that did the trick. Good to know it’s not an issue related to namespacing.

What about scenarios where this same error is occurring, outside of a conditional or inside a for loop? For example, these two blocks of code:

Block 1:
$jobs[$i]->lists[0]->content and $jobs[$i]->lists[1]->content are undefined

<?php
    for ($i = 0; $i < count($jobs); $i++) {
      echo '
      <div class="tab-pane fade" id="' . $jobs[$i]->slug . '">
        <ul class="job-respons hidden">' . $jobs[$i]->lists[0]->content . '</ul>
        <ul class="job-require hidden">' . $jobs[$i]->lists[1]->content . '</ul>
    </div>';
    }
?>

Block 2:
if($one->id == $postdata[‘jobid’]) is returning undefined, causing the h2 content not to render.

<?php
    $cur_job = null;
    foreach($jobs as $one) {
        if($one->id == $postdata['jobid']) {
            $cur_job = $one;
            break;
        }
    }
?>

<h2 class="job-title"><?php echo htmlentities($cur_job->text); ?></h2>