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

Trevor Wood
Trevor Wood
17,828 Points

Code Challenge: Objects (PHP)

I'm stuck on this code challenge. I'm not sure where to put the properties of an object.

The Question: PalprimeChecker objects have a property called number. This task has two parts. First, assign that property a value of 17. Second, remove the two hash signs (##) in the first echo statement and instead concatenate the value of this number property. (Be sure to preserve the space after the two hash signs by concatenating a space after the number.)

My Answer:

<?php

include("class.palprimechecker.php");
$checker = new PalprimeChecker();
$checker->PalprimeChecker(17);





echo "The numbe" . " " . $checker;
echo "(is|is not)";
echo " a palprime.";
?>

Any ideas?

4 Answers

Piotr Aszoff
Piotr Aszoff
8,150 Points

Hi, try to read once again, and as you can see you have to use 'number' method, but as I can see you are trying to force PalprimeChecker constructor to pass a value.

Imagine that you have a class 'Test' which has a 'name' method. Then you are making a new instance for example

$newtest = new Test();

Now, to pass something to 'name' method, you have to write $newtest->name = "Steven";

And than to get the value echo $newtest->name

That's it.

So going back to the exercise use:

$checker->number = 17; and then echo "The number ".$checker->number;

Hope it helps, regards, Piotr

Trevor Wood
Trevor Wood
17,828 Points

hmm, I tried that but it's still not accepting the answer.

<?php

include("class.palprimechecker.php");
$checker = new PalprimeChecker();
$checker->number = 17;





echo "The number" . " " . $checker;
echo "(is|is not)";
echo " a palprime.";

?>
Piotr Aszoff
Piotr Aszoff
8,150 Points

Almost there :)

You have to change here

echo "The number" . " " . $checker;

as well.

$cheker is your instance, but you did not use a method

Trevor Wood
Trevor Wood
17,828 Points

ah! alright. I thought it just changed the variable. Thanks Piotr!

Piotr Aszoff
Piotr Aszoff
8,150 Points

No problem, glad I could help

I believe you assign the number when instantiating 'new PalprimeChecker();'

(A link to the actual quiz would be helpful)

Something like this:

          <?php

include("class.palprimechecker.php");
$checker = new PalprimeChecker(17);

echo "The number" . $checker ." ";
echo "(is|is not) a palprime.";
?>
Trevor Wood
Trevor Wood
17,828 Points

I tried this one too, but still no luck. Here's the link

Matthew Underhill
Matthew Underhill
20,361 Points

For anyone reading this who is still stuck on this challenge, I have just worked it out. Caught me out for a few minutes though.

When you echo the value of the $checker variable, you have to specify the property as well. See my code below which passed correctly.

<?php

include("class.palprimechecker.php");

$checker = new PalprimeChecker();

$checker->number = 17;

echo "The number" . $checker->number . " ";
echo "(is|is not)";
echo " a palprime.";

?>
<?php
require_once('class.palprimechecker.php');

$checker = new PalprimeChecker;

$checker->number=17;

echo "The number " . $checker->number . " ";
echo "(is|is not)";
echo " a palprime.";
?>

I was stuck on this for a while. This worked for me.