PHP Build a Basic PHP Website Adding a Basic Form Objects

Cliff Jackson
Cliff Jackson
2,886 Points

Can't assign object to variable?

can't seem to assign the value not sure what is wrong?

palprimes.php
<?php
include ("class.palprimechecker.php");
$checker->value = 'PalprimeChecker';





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

?>

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, Cliff Jackson ! I feel like you're missing what they're asking here. We use the arrow (->) when we're referring to a property on an instance of an object. In your code, you are trying to assign the string "PalprimeChecker" to the value property of some instance of some object. But you haven't yet made`an instance of an object, which is what this task is asking for.

$checker = new PalprimeChecker;

This declares a variable named $checker and will hold a new instance of the PalprimeChecker as an object. Thus, the value of the variable is a new instance of an object.

Hope this helps! :sparkles:

Cliff Jackson
Cliff Jackson
2,886 Points

Thanks Jennifer, i did not quite understand the question properly, thinking it was object related as covered in previous videos. Similar to assigning functions then but with the 'new' inserted before.?

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

Hi there, Cliff Jackson ! It is object related. That is what the new keyword does. It creates a new instance of that object. Let's say for example that the object is a student and that student has a name, a grade point average, a birthdate etc. For each individual student, you would need to make a new instance of an object of the "Student" class.

At about 1:30 of this video you can see the creation of a new instance of PHPMailer which is then assigned to the variable $mail.