I'm having some trouble accessing the properties of a passed object in one of my functions. I know there is not an issue with the object definition or how I am accessing the properties, because I am able to do that elsewhere just fine.
For reference:
$officials_array
is an array of Official
objects.
$division
and division_scope
are properties of this object.
$modal_counter
is just a counter (I have simplified the code down to just what is giving me issues, so as you can see the counter isn't actually modified in this code).
Here is the code:
$modal_counter = 0;
function echoOfficial($offical, $modal_counter) {
echo "Scope: " . $official->division_scope . " Name: " . $official->division;
return $modal_counter;
}
echo "<h2>National</h2>";
foreach ($officials_array as $official) {
if ($official->division_scope == "national") {
$modal_counter = echoOfficial($official, $modal_counter);
}
}
The if
works fine, and the echoOfficial()
function gets called, but the properties are not echoed along with the text (the output is just Scope: Name:
), and it seems that the object does not actually get passed to the function. If I try to access one of the object's methods, the code just doesn't run.
I can't figure out what I am doing wrong. I think it may have something to do with accessing the objects from a foreach loop, but I am not sure.