标签:des style class blog code http
php 面向对象编程
1:创建类 class
<?php class SimpleClass { //define var public $var = ‘a default value‘; //method define public function displyVar() { echo $this->var; } } //SimpleClass::displyVar(); $obje1=new SimpleClass(); $obje1->displyVar(); ?>
2: 类的继承 extends
<?php class Person { public $city="济南"; //成员方法不能用类名调用 ,要用对象调用 public function sayHi($name=‘your name‘) { echo $name.‘say: hello everyone! I am from ‘.$this->city; echo "</br>"; } //static 是静态方法,可以用类名调用 Person::hi() static function hi($name=‘jack‘) { echo $name.‘say: hello everyone! I am from ‘; echo "</br>"; } } $p1 = new Person(); $p1->sayHi(); $p1->city="北京"; $p1->sayHi("cocoa"); Person::hi("tom"); Person::hi(); echo "some city".$p1->city; class Student extends Person { public $name="姓名"; public function study() { echo $this->name."good good study!"."</br>"; } } echo "<hr>"; ?>
3:类的访问权限设置 public,private,protected
<?php /** * 访问权限 public : 公开的,子类,处部都可以访问,对象等可以访问 private: 私有的,只有在本类内部访问,子类外部不可访问,对象不可访问 protected:受保护的,子类可以访问,外部不可访问,对象也不可以访问 */ class A { public $public = "public var"; private $private = "private var"; protected $protected = "pritected var"; function hi() { echo $this->public."</br>"; echo $this->private."</br>"; echo $this->protected."</br>"; } } $a = new A(); $a->hi(); echo "<hr>"; echo $a->public."</br>"; //可访问 //echo $a->protected."</br>"; //不可访问 //echo $a->private."</br>"; //不可访问 echo "<hr>"; class B extends A { //可以对 public 和 protected进行重新定义,但是 private 的不能; //protected $protected = "protected2"; function hi2() { echo $this->public."</br>"; echo $this->private."</br>"; echo $this->protected."</br>"; } } $b = new B(); $b->hi(); $b->hi2(); ?>
4:抽象类 abstract
<?php /** php抽象类 abstract 抽象类不能直接被实例化,必须由子类继承去实现; 抽象类必须至少有一个抽象方法; 抽象类可以有非抽象方法; */ abstract class A { //强制要求子类实现的方法 abstract public function method(); abstract protected function method2(); //抽象方法不能声明为private //abstract private function method4(); public function method3() { echo "method3 </br>"; } } class B extends A { public function method() { echo "method </br>"; } protected function method2() { echo "method2"; } public function hi() { $this->method2(); } } $b = new B(); $b->method(); $b->method3(); $b->hi(); ?>
5: 接口实现 interface,implements
<?php /** php 接口 指定某个类必须实现哪些方法; 接口中的方法必须是 public的 关键字 interface 一个类可以实现多少个接口 接口中的可以定义常量,都是定值,不能修改 */ interface A { //接口的方法,必须都是public的 public function methodA(); public function methodB(); } interface C { //接口里面声明常量,不可改变 const varIP = ‘192.168.8.35‘; public function methodC(); } //实现 class B implements A,C { public function methodA() { echo "implements methodA </br>"; } public function methodB() { echo "implements methodB </br>"; } public function methodC() { echo "implements methodC</br>"; } } $b = new B(); $b->methodA(); $b->methodB(); $b->methodC(); //常量访问 echo C::varIP; ?>
6: 构造方法: __construct()
<?php class B { function __construct() { echo "this is B init method</br>"; } } $b = new B(); class C extends B { //继承关系的话,会先调用父类的 构造方法,然后执行自己的构造方法; //也可以自己手动调用父类的构造方法,关键字 parent:: function __construct() { parent::__construct(); echo "this is C init method </br>"; } } $c = new C(); ?>
7: 析构方法 __destruct()
<?php class B { function __construct() { echo "this is B init method</br>"; } function __destruct() { echo "this is B Class Destroying</br>"; } } //$b = new B(); class C extends B { //继承关系的话,不会先调用父类的 构造方法,需要自己手动调用 ,然后执行自己的构造方法; //手动调用父类的构造方法,关键字 parent:: function __construct() { parent::__construct(); echo "this is C init method </br>"; } //析构造方法也是,子类不会主动调用父类的析构方法,要手动调用 function __destruct() { parent::__destruct(); echo "this is C Class Destroying</br>"; } } $c = new C(); ?>
8: 常量 const , self,$this
<?php class A { function foo() { if (isset($this)) { echo ‘$this is defined (‘; echo get_class($this); echo ") "; } else { echo "$this is not defined. "; } } } class B { function bar() { A::foo(); } } $a = new A(); $a->foo(); A::foo(); $b = new B(); $b->bar(); B::bar(); ?> <?php class Person { const IP_ADDRESS= "192.168.8.35"; public function hi() { echo self::IP_ADDRESS."</br>"; } //self 是指类本身,可调用类方法和常量; //$this 是的是 new出来的对象,可调用对象属性和成员方法; } echo Person::IP_ADDRESS."</br>"; $p = new Person(); $p->hi(); ?>
9: 对象迭代 foreach
<?php /** 对象迭代 php提供了对象迭代功能,就像数组那样,可以通过foreach来遍历对象中的所有属性, 所有外部可见的属性; */ class A { public $var1 = ‘var 1‘; public $var2 = ‘var 2‘; protected $var3 = ‘protected var 3‘; private $var4 = ‘private var 4‘; function allVar() { echo ‘<hr>‘; foreach($this as $key=>$var) { echo $key.‘->‘.$var."</br>"; } } } $a = new A(); //外部迭代 foreach($a as $key=>$var) { echo $key.‘->‘.$var.‘</br>‘; } //内部迭代 $a->allVar(); ?>
标签:des style class blog code http
原文地址:http://www.cnblogs.com/cocoajin/p/3808009.html