传统的构造函数:
class Student{ public $Name; public $Sex; //默认是public public function Student() { echo "传统的构造函数(php4.0)"; } } $stu = new Student();
输出:
传统的构造函数(php4.0)
php>=4.0构造函数:
class Student{ public $Name; public $Sex; public function __construct($iName,$Sex)//两个下划线!! { $this->Name = $iName; $this->Sex = $Sex; echo "php4>=4.0的构造函数,且优先级比传统的高"; } } $stu = new Student("张三",22); echo "<br/>Name=".($stu->Name).", Sex=".($stu->Sex);
输出:
php>=4.0的构造函数,且优先级比传统的高
Name=张三, Sex=22