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!
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
Trevor Wood
17,828 PointsCode 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
8,150 PointsHi, 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

Riley Hilliard
Courses Plus Student 17,771 PointsI 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
17,828 PointsI tried this one too, but still no luck. Here's the link

Matthew Underhill
19,607 PointsFor 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.";
?>

Cesar De La Vega
13,900 Points<?php
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker;
$checker->number=17;
echo "The number " . $checker->number . " ";
echo "(is|is not)";
echo " a palprime.";
?>

Cesar De La Vega
13,900 PointsI was stuck on this for a while. This worked for me.
Trevor Wood
17,828 PointsTrevor Wood
17,828 Pointshmm, I tried that but it's still not accepting the answer.
Piotr Aszoff
8,150 PointsPiotr Aszoff
8,150 PointsAlmost 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
17,828 PointsTrevor Wood
17,828 Pointsah! alright. I thought it just changed the variable. Thanks Piotr!
Piotr Aszoff
8,150 PointsPiotr Aszoff
8,150 PointsNo problem, glad I could help