Here is the context. There is route which calls this snippet:
$b = (object) [
'c' => null,
'p' => null,
'r' => null
];
// $x is passed from the route as GET parameter, and is valid and correct at this point
if ($x) {
$x = autoid($x);
if ($x) {
if ($x->x_excl_cats()->isNotEmpty()) {
$b->c = Str::split($x->x_excl_cats());
}
if ($x->x_excl_products()->isNotEmpty()) {
$b->p = Str::split($x->x_excl_products());
}
if ($instance->x_excl_ranges()->isNotEmpty()) {
$b->r = Str::split($x->x_excl_ranges());
}
}
}
// $b and $b->c is valid and correct at this point, but undefined in the snippet
// $a is passed from the route as GET parameter and is correct and usable within the snippet
$categories = snippet('products/categories', ['a' => $a, 'b' => $b->c], true);
Within the snippet, $a (equal to $a string) is correct, and $b (equal to $b->c array) is undefined.
Have also tried passing 'b' => $b->c as a string, and it’s still undefined.
Yeah, that’s fine - the snippet can handle $b being null, but the issue is it being undefined inside the snippet when it’s definitely not when I pass it in.
Strange. How do you access $b within your snippet? You can perhaps try
<?php
$a = $a ?? "";
$b = $b ?? []; // or ?? null
?>
<?php /* Your snippet code goes here. $b should definitely be defined by now. */ ?>
You can then try to figure out why the real value of $b does not make its way into the snippet. Maybe there’s a name clash anywhere else. Using one-letter variable names can be prone to such effects.