I can’t echo objects

Asked

Viewed 153 times

0

I’m making an object-oriented system with and I noticed that the following code does not work:

<?php
include 'User.php';

$temp_user = new User();
$temp_user->set_name(htmlspecialchars($_POST['name']));
$temp_user->set_user_name(htmlspecialchars($_POST['username']));
$temp_user->set_password(htmlspecialchars($_POST['password']));

//$local_name= $temp_user->get_name();

echo "<br>$temp_user->get_password()<br>"
?>

But the same code without sending directly the class method works:

include 'User.php';

$temp_user = new User();


$temp_user->set_name(htmlspecialchars($_POST['name']));
$temp_user->set_user_name(htmlspecialchars($_POST['username']));
$temp_user->set_password(htmlspecialchars($_POST['password']));

//$local_name= $temp_user->get_name();

$var1 = $temp_user->get_password();

echo "<br>$var1<br>"
 . "<br><br>"
 . "<br><br>";
?>

What could be wrong?

NOTE: all my methods are public

  • 2

    Try to put keys in the first code: {$temp_user->get_password()}

  • 1

    In the first code. Or do so: echo "<br>".$temp_user->get_password()."<br>"

  • 1

    Within the echo. Something like echo "<br>{$temp_user->get_password()}</br>"

1 answer

2


Hello, PHP cannot understand objects inside the string.

A solution: in the first code do the following: echo "<br>". $temp_user->get_password()." <br>";

Will work.

  • 1

    Can you explain why the current code didn’t work and why it works? If you do, please edit the answer, not in the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.