Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Saajan Bedi
Saajan Bedi
12,174 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
8,110 Points

Dang, thank you!