0

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.

Conor Strejcek
  • 382
  • 2
  • 15

1 Answers1

2

You have a simple typo - function echoOfficial($offic!!al - you missed i.

I'd recommend you use any syntax-highlighting IDE to avoid such mistakes in future, as they are incredibly hard to detect otherwise.

Eternal1
  • 5,447
  • 3
  • 30
  • 45
  • for these typos if you use a good ide, will have no issues like this at 99% rate. most ides will give a red indicator at the line or etc. – Santa's helper Sep 22 '14 at 13:47
  • 1
    @ConorStrejcek The IDE is one thing, think to enable errors too. [How to get useful error messages in PHP?](http://stackoverflow.com/a/845025/3361444) – Debflav Sep 22 '14 at 13:47