I can not echo objects

0

I'm doing 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 directly sending 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

    
asked by anonymous 05.08.2017 / 15:58

1 answer

2

Hello, PHP can not understand objects inside the string.

One solution: in the first code do the following: echo "
" get_password (). "
"

It will work.

    
05.08.2017 / 17:59