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 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!!!