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
Matthew Mascioni
20,444 PointsBuild a Simple PHP Application > Wrapping up the Project > Objects Code Challenge
Hey guys! Hoping someone can help me out with a code challenge I'm stuck on. It's the Objects Code Challenge in the "Wrapping up the project" stage of "Build a Simple PHP Application". The question is:
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.)
The code I put in was:
<?php
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker;
$checker -> number(17);
echo "The number $checker".' ';
echo "(is|is not)";
echo " a palprime.";
?>
It's not really working, as I get:
PHP Fatal error: Call to undefined method PalprimeChecker::number() in palprimes.php on line 7
Could anyone help me out a little?
Merry Christmas :)
11 Answers

Randy Hoyt
Treehouse Guest TeacherThe conditional looks great, and the space-time continuum appears to be intact! :~)
First of all, what is the output that you see? Is there a PHP error?
Second of all, I see two issues with the code before the conditional, and I suspect addressing them will fix the output.
(1) When you assign the value to the number property, you shouldn't have any spaces between the object name, the single arrow, and the property:
$checker->number = 17;
(2) In the sentence "The number ## ...", you want to echo out the value in the number property, not the whole object:
echo "The number " . $checker->number . " ";
Does that help?

Randy Hoyt
Treehouse Guest TeacherHey Matthew,
Properties are like variables within an object. Take a look back at the video with the PHPMailer object. It has a property named "subject," and we assigned it a value like this:
$mail->subject = "Subject";
Does that help?

Matthew Mascioni
20,444 PointsHey Randy,
That helped a lot! When I go to task 5/7 however, and provide this code:
<?php
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker;
$checker -> number = 17;
echo "The number " .$checker. " ";
if($checker->isPalprime()) {
echo "is";
} else {
echo "is not";
}
echo " a palprime.";
?>
I'm passing it as a method of $checker, with no arguments (and using that single arrow that you taught in the video), but the preview shows up completely wrong. Have I broke the space-time continuum?

Matthew Mascioni
20,444 PointsRandy,
Originally, the output was simply a blank page (in the preview window). This was probably because I was messing up my echo statement.
Your suggestions worked perfectly! My spaces and not using $checker->number was the problem.
Thanks a lot! Can't wait to finish the project :D

Daniel Sullivan
5,187 PointsHi, I didn't want to start a new thread since it's in the same section. I just started the "Building A Simple PHP App." When I change the index.html file to index.php it doesn't load in my browser. So far I have followed the two directions of changing the date and title to php code. Any ideas? I tried it in Chrome and Firefox, running mac 10.8.
-Dan

Daniel Sullivan
5,187 PointsSolved. I left a space between my ? and php, learned a lesson on this one : )

Randy Hoyt
Treehouse Guest Teacher@Daniel,
Sweet. Glad you got it working! (I think starting a new thread here would have made sense, since you're asking about a different part of the project.) I hope you enjoy the course: let me know if you have any questions!

Emily Williams
Courses Plus Student 3,968 PointsI didn't want to start a new thread but I had a quick question about my code is it better to have
<?php
$num = 17;
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker -> number = $num;
echo "The number $num ";
if(!$checker -> isPalprime()) {
echo "is not Palprime";
} else {
echo "is Palprime";
}
?>
or to have it like this
<?php
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker -> number = 17;
echo "The number " . $checker->number . " ";
if(!$checker -> isPalprime()) {
echo "is not Palprime";
} else {
echo "is Palprime";
}
?>
Both codes work and I get the same result just wondering what is more universal Thanks

Randy Hoyt
Treehouse Guest TeacherHey @Emily,
I don't like this line from the first example:
echo "The number $num ";
The $checker->isPalprime()
method is going to check the value in $checker->number
, so it makes much more sense to display the value from there instead of the value in the $num
variable. (They have the same value right now, so it works: but it's possible that a line of code could added in the future to make them diverge.)
The other issue is whether you need a variable called $num to hold the value 17. It depends on what makes the code easier to maintain. If someone is going to be changing the value in the code periodically, it might be night to define the value at the top of the file like you have done in the first example. With this code block, though, I don't think that's an issue: I'd recommend your second example.
Does that help?

Nicolas Pocock
2,763 PointsI'm trying to complete the code challenge, however it keeps telling me my code isn't output correctly, when I preview it looks fine to me, can anyone see where i'm going wrong?
<?php
include ("class.palprimechecker.php");
$checker = new PalprimeChecker();
$checker->number = 17;
echo "The number " . $checker->number . " ";
if (!$checker->isPalprime()) {
echo " is not Palprime";
} else {
echo " is Palprime";
}
?>
EDIT
I worked out why, it was because I should of done it like this if($checker->isPalprime()) { echo "is"; } else { echo "is not"; } echo " a palprime."

M C
12,105 Pointsinclude('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker->number = 17;
echo "The number " . $checker->number . " ";
if ($checker->isPalprime()) {
echo "is";
} else {
echo "is not";
}
echo " a palprime";
Got this code running but can`t pass the task any advice?
Michael Moore
7,121 PointsMichael Moore
7,121 Pointsrather than having to write:
I just assigned it a variable for easy reference.