Php version is 7.1.
I want to initialize class fields with values, but it fails.
Why does php in a class constructor in a field assign the last value?
class Order
{
private $yourname;
private $yourphone;
public function __construct(string $p_yourname, string $p_yourphone)
{
$this->$yourphone =$p_yourphone;
$this->$yourname=$p_yourname;
echo $p_yourname;
echo $p_yourphone;
echo $this->$yourphone;
echo $this->$yourname;
}
}
call conctructor
$order = new Order( 'Andry', '+79777475675');
Result in echo is: Andry+79777475675AndryAndry
2
Answers
Clear. The parameter name and the field name in the constructor must be the same. The syntax for assignment in the constructor should be like this $this->yourphone =$yourphone;
That's right so
Correction: