Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Designing Interfaces in PHP Abstract Classes Finishing Our Site

Ray Knag
Ray Knag
8,110 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

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
8,110 Points

Dang, thank you!