php classのオブジェクトをInstantiateしてClass内Public VariableのEchoの仕方
<?php
$name = "Nobu Kim";
$age = 42;
$user1 = new User('Nobu', '42');
echo $user1->username; //doesn't get printed
echo $user1->age; //doesn't get printed
//print_r($user1);
//test->__destruct(); cannot call explicitly
echo "wtf"; //gets printed
echo $name; //gets printed
//echo $user1;
class User
{
public $username;
public $age;
function __construct($name, $age)
{
//Constructor statements here
$username = $name;
echo $username; //gets printed
$age = $age;
echo $age; //gets printed
}
}
function __destruct()
{
//Destructor code here
}
?>
上記、何故コメントにて”Doesn't get printed"のライン、echo $user1->username
& echo $user1->age;
はプリントされないのでしょうか。