I'm working through a course on PHP and I'm completely stuck on one of the tasks.
The bit i'm stuck on is:
"PalprimeChecker [a function created just for this task] objects have a property called number. This task has two parts. First, assign that property a value of 17...."
The code I've written is returning an error and won't let me progress...
include('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker->number = '17';
echo "The number " . "$checker";
echo "(is|is not)";
echo " a palprime.";
I'm not sure at all where I'm going wrong with this. Anyone know the correct way to assign this value?
Hope you can help as I'm tearing my hair out!
Thanks!
Modify this :
echo "The number " . "$checker";
By This :
echo "The number " . $checker->number;
You were trying to print the entire object (which isn't possible without creating a method for it, check orangePill's answer for this), what you wanted to do is simply print the number inside the object.
Also note that you don't need to use quotes when assigning numbers. It might cause issues later on. You should simply assign it like this :
$checker->number = 17;
You can also add a __toString method on the PalprimeChecker class.
public function __toString(){
return (string)$this->number;
}
This will allow echo "The number " . $checker;
to produce a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With