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 Object-Oriented PHP Basics Building a Collection Enhancing the Collection

Ronaldo Fialho
Ronaldo Fialho
5,105 Points

I'm getting an error, and can't figure this out! undefined method. and it's defined inside of class public function

What is wrong here? help?

treehouse:~/workspace$ php cookbook.php
PHP Fatal error: Uncaught Error: Call to undefined method Recipe::getTitle() in /home/treehouse/wor kspace/classes/RecipeCollection.php:37
Stack trace:

0 /home/treehouse/workspace/cookbook.php(26): RecipeCollection->getRecipeTitles()

1 {main}

thrown in /home/treehouse/workspace/classes/RecipeCollection.php on line 37

Fatal error: Uncaught Error: Call to undefined method Recipe::getTitle() in /home/treehouse/workspac e/classes/RecipeCollection.php:37
Stack trace:

0 /home/treehouse/workspace/cookbook.php(26): RecipeCollection->getRecipeTitles()

1 {main}

thrown in /home/treehouse/workspace/classes/RecipeCollection.php on line 37

<?php

class RecipeCollection { private $_title; private $_recipes = array();

public function __construct($title = null) { $this->setTitle($title); }

public function setTitle($title) { if(empty($title)) { $this->_title = null; } else { $this->_title = ucwords($title);
} } public function getTitle() { return $this->_title; }

public function addRecipe($recipe) { $this->_recipes[] = $recipe; }

public function getRecipes() { return $this->_recipes;
}

public function getRecipeTitles() {

$titles = array();    
foreach ($this->_recipes as $_recipe) {
  $titles[] = $_recipe->getTitle();
}
return $titles;

} }

1 Answer

Hi there Ronaldo Fialho!

Do not be afraid to read the error description. It doesn't bite;)

In this case problem is next:

*PHP Fatal error: Uncaught Error: Call to undefined method Recipe::getTitle() *

You are trying to invoke method getTitle() from the array element. Which is not correct. if you want to get object then you need to have array of objects that can invoke such method. Another small thing concerns property _title

 public function getTitle() { return $this->title; }

should be rewritten to this(you've forgot about underscore)

 public function getTitle() { return $this->_title; }

But, if you want to invoke method from foreach loop, you need to redo your code a little.

public function addRecipe($recipe) { $this->_recipes[] = $this; }

 public function getRecipeTitles() {

      $titles = array();    
      foreach ($this->_recipes as $_recipe) {
         $titles[] = $_recipe->getTitle();
      }
      return $titles;
 } 

Above is working version of your code. But it is a mistery to me what are you trying to achieve =)

class RecipeCollection { 
    private $_title; 
    private $_recipes = array();

    public function __construct($title = null) { $this->setTitle($title); }

    public function setTitle($title) { 
        if(empty($title)) { $this->title = null; } 
        else 
        { $this->_title = ucwords($title);} 
    } 

    public function getTitle() { return $this->title; }

    public function addRecipe($recipe) { $this->_recipes[] = $recipe; }

    public function getRecipes() { return $this->_recipes; }

    public function getRecipeTitles() {

      $titles = array();    
      foreach ($this->_recipes as $_recipe) {
         $titles[] = $_recipe;
      }
      return $titles;
    } 
}

$recipe = new RecipeCollection('One');
$recipe->addRecipe('some desсription');
print_r($recipe->getRecipeTitles());

Hope that this will explain few things.

Best regards.

Ronaldo Fialho
Ronaldo Fialho
5,105 Points

Yes, that helped. Thank you for your time!!!