PHP Designing Interfaces in PHP Abstract Classes Finishing Our Site

Ray Knag
Ray Knag
7,012 Points

Cannot get nav to work for this lesson

I am having trouble getting Navigation to work for this lesson, I am using the following code in header.php and I am pretty sure it matches what's in the video after watching it a few times. I cannot help but wonder if I am missing something in another file since the error indicates that stdClass could not be converted to a string. My code is:

                <ul class="navbar-nav mr-auto">
                    <?php
                      foreach (new Pages($repo) as $page) {
                        echo '<li class="nave-item';
                        if (isset($slug) && $slug == $page->slug) {
                          echo ' active';
                        }
                        echo '">';
                        echo '<a class="nav-link" href="index.php?slug='
                           . $page . slug . '">';
                        echo ucwords(str_replace('-', ' ', $page->slug));
                        echo '</a></li>';
                      }
                    ?>
                </ul>
                <ul class="navbar-nav mt-2 mt-md-0">
                    <li class="nav-item<?php
                    if ($_SERVER['SCRIPT_NAME'] == '/blog.php') {
                      echo ' active';
                    }
                    ?>">
                      <a class="nav-link" href="blog.php">Lastest Blog Posts</a>
                    </li>

The error I get is this:

"Catchable fatal error: Object of class stdClass could not be converted to string in /home/treehouse/workspace/views/header.php on line 35"

I am going to take a look at this a little later with fresh eyes but if someone could help me get on the right track about where to look for this it would be great!

1 Answer

Saajan Bedi
Saajan Bedi
11,917 Points

You're not using object in right manner

<?php
echo '<a class="nav-link" href="index.php?slug='
                           . $page . slug . '">';
// correct way
$page->slug;

?>

Ray Knag
Ray Knag
7,012 Points

Dang, thank you!