以下将涵盖php面向对象的主要知识点:
创建classname.php
$name; } public function __set($name,$value){ if(($name=attribute) && ($value>=0) && ($valueattribute=$value; } }}//访问修饰符:private、protected、public;class a{ public $attribute=default value; private function operation1(){ echo operation1 called
; } protected function operation2(){ echo operation2 called
; } public function operation3(){ echo operation3 called
; } function operation(){ echo something
; echo the value of \$attribute is . $this->attribute.
; }}//类的继承、重载class b extends a{ public $attribute=different value; function __construct(){ $this->operation2(); $this->operation3(); } function operation(){ echo something else
; echo the value of \$attribute is . $this->attribute.
; }}//使用final关键字禁止继承和重载class finala{ public $attribute=default value; final function operation(){ echo something
; echo the value of \$attribute is.$this->attribute.
; }}//实现接口interface displayable{ function display();}class webpage implements displayable{ function display(){ echo 实现了接口
; }}//使用pre-class常量、实现静态方法class math{ //使用pre-class常量 const pi=3.14159; //实现静态方法 static function squared($input){ return $input*$input; }}//延迟静态绑定class c{ public static function who(){ echo __class__; } public static function test(){ static::who(); }}class d extends c{ public static function who(){ echo __class__; }}//使用抽象类abstract class e{ abstract function operationx($param1,$param2);}//使用__call()重载方法class overload{ public function __call($method,$p){ if($method==display){ if(is_object($p[0])){ $this->displayobject($p[0]); }elseif(is_array($p[0])){ $this->displayarray($p[0]); }else{ $this->displayscalar($p[0]); } } }}//实现迭代器和迭代class myclass{ public $a=5; public $b=7; public $c=9;}//将类转换成字符串class printable{ public $testone; public $testtwo; public function __tostring(){ return(var_export($this,true)); }}
创建index.php
attribute=5;//类的继承、重载$b=new b();$b->operation3();echo
;$b->operation();//访问常量所属的类echo math::pi=.math::pi.
;//访问静态方法echo math::squared(8).
;//检查类的类型和类型提示 instanceof关键字if($b instanceof b){ echo true
;}function check_hint(b $someclass){ echo right
;}check_hint($b);//延迟静态绑定d::test();echo
;//克隆对象$c=clone $b;//使用__call()重载方法$ov=new overload();$ov->display(array(1,2,3));$ov->display('cat');//使用__autoload()方法function __autoload($name){ include_once $name..php;}//实现迭代器和迭代$x=new myclass();foreach($x as $attribute){ echo $attribute.
;}//使用reflection(反射)apirequire_once (tla/page.inc);$class=new reflectionclass(page);echo .$class.
;
以上就介绍了php的面向对象,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。