This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course » View Exercise

316 points
2d16755c9dfbc4d795ea0b94b8163165?s=140&d=retro
Submitted by
Giovanni Tempobono
over 4 years ago

My meow method does not return anything on PHP OOP I lesson 09/10

Hi,

I am having problems with my meow() method on lesson 09/10 of PHP OOP I, which prints "Meow meow" on the screen but it gives me the error "Oops, try again! Your 'meow()' method does not 'return' anything."

    <?php
      class Cat {
          public $isAlive = "true";
          public $numLegs = 4;
          public $name;
          public function __construct ($name) {
              $this->name;
          }
          public function meow() {
              echo "Meow meow";
          }
      } 

          $cat1 = new Cat("CodeCat");
          $cat1->meow();


    ?>

Is this some kind of bug? can you guys help me?

Thank you,
Giovanni


1 vote

permalink

It's probably because you're not assigning anything to name in your constructor.

i.e.:
public function _construct ($name) {
$this->name;
}
should be:
public function _
construct ($name) {
$this->name = $name;
}

1107 points
936f3810be0442010950c97f1b151889?s=140&d=retro
Submitted by
Nick Savov
over 4 years ago


0 votes

permalink

Hello,
does it return anything? ;-)
You ought to use return and then echo. ;-)

3108 points
5154c6cf115b457ac9000021_667301409
Submitted by
boring12345
over 4 years ago

2 Comments

Df937fdf9871e16b784366e54a688006?s=140&d=retro xpo60rj over 4 years ago

Isn't the purpose of return to not output anything to the browser but echo is?

5154c6cf115b457ac9000021_667301409 boring12345 over 4 years ago

I don't really understand your point. Usually you use return, so your result can be accessed from everywhere. If you print it, you won't be able to access it anymore. So you can use return in the method and echo afterwards.


0 votes

permalink

I think putting echo inside the class won't print anything. You need $this->meow() for method or $this->meow for a property to access a property or method of the class. It was taught in some previous lesson. I think this is a fixed rule for accessing classes. So maybe you just need to create a return in the class and then access the method with a $this->meow();
where $this represents the object.

But what the OP has written in the meow() function seems to be legit too. Now I'm confused :D

646 points
Picture
Submitted by
sunoy14
over 2 years ago