public function Say(){ echo 'Hello, My name is',$this->name,' My Sex is',$this->gender; //函数内调用必须使用$this } //PHP函数也不能被重载 /* public function Say($hi){ echo 'Hello, My name is',$this->name,' My Sex is',$this->gender; //函数内调用必须使用$this echo '<br />', $hi; } */ } $p1 = new Human('牛xx', '男'); $p2 = new Human('空姐', '女'); //$p3 = new Human('小xx', '男', 18) $p4 = new Human('性感小姐', '女'); $p1->Say(); echo '<br />'; $p2->Say(); echo '<br />'; //$p3->Say(); //Fatal error: Cannot redeclare Human::__construct() $p4->Say(21); ?>
<?php /* PHP和java,c++相比 方法体内想访问 调用者的属性,必须用$this,如果不加,则理解为方法内部的一个局部变量。 */ header('Content-Type:text/html;charset=utf-8'); class Human{ public $name = 'nobody'; public function Say(){ echo 'Hello, My Name is',$this->name,'<br />'; } } $p1 = new Human(); $p1->Say(); ?>