Can't use variables on my Sage Theme :O

I’m using Sage 10. In a fresh new installation I can’t use variables in PHP.
Example:

This code works, if I remove line 14 from comment it says “Argument #1 must be of type array, null given”. What’s going on? :open_mouth:

Same in other Sage projects.
This snippet is from a php file under /app

This isn’t a problem with Sage. This isn’t valid PHP.

$default_labels is outside of the scope of the function being registered to add_action.

If you move $default_labels into scope it will work:

<?php

add_action('init', function () {
  $default_labels = ['foo' => 'bar'];
  register_extended_post_type('service', [], $default_labels);
});
3 Likes

If you’d like to reuse $default_labels in multiple hooks, you could also use to bring $default_labels into scope.

$default_labels = ['foo' => 'bar'];

add_action('init', function () use ($default_labels) {
  register_extended_post_type('service', [], $default_labels);
});
2 Likes