class classname { public $color; public $age; 访问修饰符 function __construct($param_color,$param_age){ $this->color = $param_color; $this->age = $param_age;//$this代表调用这个构造函数的对象 } } $coco = new classname('red','12');//在创建这个新的对象Coco时系统自动完成了对象的属性定义
class Dog { public $name; public $age; public $sex; public function __construct($name,$sex){//这里没有问题,$sex只是个参数哦,什么名字都行 $this->name = $name;//但是为了方便阅读,还是尽量规范参数的命名 $this->age = $sex; } public function __set($name,$value){ $this->$name = $value; } } //在类的外部 $coco = new Dog("titi",1); echo $coco->name; echo $coco->age; $coco->age = 2; $coco->name="coco"; echo $coco->name; echo $coco->age;
{ public $name; private $age; public function __construct($value1,$value2){ $this->nmae = $value1; $this->age= $value2; } public function __get($name){ return $this->$name; } } //在类的外部 $coco = new Dog("titi",1); echo $coco->name; echo $coco->age;
__isset() 调用条件:在外部 isset()受保护的或者私密的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Dog { public $name; private $age; public function __construct($name,$age){ $this->nmae = $name; $this->age = $age; } public function __isset($name){ isset($this->$name); } } //在类的外部 $coco = new Dog("titi",1); var_dump(isset($coco->age));//这个返回true 或者faulse
__unset 调用条件:在外部释放受保护的或者私密的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Dog { public $name; private $age; public function __construct($name,$sex){ $this->nmae = $name; $this->sex = $sex; } public function __unset($name){ unset($this->$name); } } //在类的外部 $coco = new Dog("titi",1); unset($coco->$age); var_dump(($coco);
abstract class Father{ public abstract function run();//抽象方法是没有方法体的 public function eat(){//这个就是个普通的成员方法(函数) echo "eat"; } } class son extends Father{ public function run(){ //父债子偿 }
interface C { function run();//因为公有是默认值,so略 } interface E { function walk(); } class D implements C,E { function run(){ echo "<br/>run"; } function walk(){ echo "<br/>walk"; } }
trait trait能在定义一个标准类的时候定义它的方法 trait SayWorld { public function sayHello() { echo 'Hello World!';//trait中的 } } class MyHelloWorld extends Base { use SayWorld;// }