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 Build a Simple PHP Application Wrapping Up The Project Objects

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Wrapping up the website task 5/7

If there ever was a code challenge that rubbed me up the wrong way, this would be it. here is the question:

"PalprimeChecker objects have a method called isPalprime(). This method does not receive any arguments. It returns true if the number property contains a palprime, and it returns false if the number property does not contain a palprime. (Tip: 17 is not a palprime.) Turn the second echo statement into a conditional that displays “is” or “is not” appropriately. The conditional should call the isPalprime method of the $checker object. If the isPalprime method returns true, then echo “is”; otherwise, echo “is not”."

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

$checker -> number = 17;

echo "The number " . $number;

if($checker-> isPalprime()) {
    echo "is ";
} else {
    echo "is not ";
} else {
    echo "a palprime. ";
}

This is my best effort yet it doesn't pass.

  1. What I want to know is how can "isPalprime" not receive arguments yet check if a number is a palprime?

  2. how is "isPalprime" checking if $checker is a prime through "($checker->isPalprime)" because it looks like $checker is assigning a property.

  3. How does "isPalprime" extract 17 from $checker since $number is just a property of the object $checker.

Any help would be appreciated.

1 Answer

Coco Jackowski
Coco Jackowski
12,914 Points

1) That "->" lets you get or set object properties just like any other variable, but it also lets you call methods, so $checker->isPalprime() is calling the isPalprime() method of $checker.

2) The isPalprime() method is a method of the object $checker, and is therefore able to 'see' the value of number, which is just another property of $checker

Maybe there is more to your code, but this line

echo "The number " . $number;

seems to include a variable that has no value and was never initialized.