php面向对象
面向对象的程序设计缩写为OOP,即Object-oriented programming
对象的主要三个特性:对象的行为、形态和表示
面向对象内容
类 对象 成员变量 成员函数 继承 父类 子类 多态 重载 抽象性 封装 构造函数 析构函数
类定义
语法:
class myclass{
…
}
类使用class关键字后加上类名定义,{}内可以定义变量(使用var来声明,也可以初始化值)和方法
创建对象
类创建后,可以使用new运算符来实例化该类的对象
调用成员方法(->用于对象执行方法或取得属性)
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?<span style="color: rgba(0, 0, 0, 1);">php </span><span style="color: rgba(0, 0, 255, 1);">class</span><span style="color: rgba(0, 0, 0, 1);"> Site { </span><span style="color: rgba(0, 128, 0, 1);">/*</span><span style="color: rgba(0, 128, 0, 1);"> 成员变量 </span><span style="color: rgba(0, 128, 0, 1);">*/</span> <span style="color: rgba(0, 0, 255, 1);">var</span> <span style="color: rgba(128, 0, 128, 1);">$url</span><span style="color: rgba(0, 0, 0, 1);">; </span><span style="color: rgba(0, 0, 255, 1);">var</span> <span style="color: rgba(128, 0, 128, 1);">$title</span><span style="color: rgba(0, 0, 0, 1);">; </span><span style="color: rgba(0, 128, 0, 1);">/*</span><span style="color: rgba(0, 128, 0, 1);"> 成员函数 </span><span style="color: rgba(0, 128, 0, 1);">*/</span> <span style="color: rgba(0, 0, 255, 1);">function</span> setUrl(<span style="color: rgba(128, 0, 128, 1);">$par</span><span style="color: rgba(0, 0, 0, 1);">){ </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->url = <span style="color: rgba(128, 0, 128, 1);">$par</span><span style="color: rgba(0, 0, 0, 1);">; } </span><span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> getUrl(){ </span><span style="color: rgba(0, 0, 255, 1);">echo</span> <span style="color: rgba(128, 0, 128, 1);">$this</span>->url . <span style="color: rgba(255, 0, 255, 1);">PHP_EOL</span><span style="color: rgba(0, 0, 0, 1);">; } </span><span style="color: rgba(0, 0, 255, 1);">function</span> setTitle(<span style="color: rgba(128, 0, 128, 1);">$par</span><span style="color: rgba(0, 0, 0, 1);">){ </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->title = <span style="color: rgba(128, 0, 128, 1);">$par</span><span style="color: rgba(0, 0, 0, 1);">; } </span><span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> getTitle(){ </span><span style="color: rgba(0, 0, 255, 1);">echo</span> <span style="color: rgba(128, 0, 128, 1);">$this</span>->title . <span style="color: rgba(255, 0, 255, 1);">PHP_EOL</span><span style="color: rgba(0, 0, 0, 1);">; } } </span><span style="color: rgba(128, 0, 128, 1);">$runoob</span> = <span style="color: rgba(0, 0, 255, 1);">new</span><span style="color: rgba(0, 0, 0, 1);"> Site; </span><span style="color: rgba(128, 0, 128, 1);">$taobao</span> = <span style="color: rgba(0, 0, 255, 1);">new</span><span style="color: rgba(0, 0, 0, 1);"> Site; </span><span style="color: rgba(128, 0, 128, 1);">$google</span> = <span style="color: rgba(0, 0, 255, 1);">new</span><span style="color: rgba(0, 0, 0, 1);"> Site; </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 调用成员函数,设置标题和URL</span> <span style="color: rgba(128, 0, 128, 1);">$runoob</span>->setTitle( "菜鸟教程"<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(128, 0, 128, 1);">$taobao</span>->setTitle( "淘宝"<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(128, 0, 128, 1);">$google</span>->setTitle( "Google 搜索"<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(128, 0, 128, 1);">$runoob</span>->setUrl( 'www.runoob.com'<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(128, 0, 128, 1);">$taobao</span>->setUrl( 'www.taobao.com'<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(128, 0, 128, 1);">$google</span>->setUrl( 'www.google.com'<span style="color: rgba(0, 0, 0, 1);"> ); </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 调用成员函数,获取标题和URL</span> <span style="color: rgba(128, 0, 128, 1);">$runoob</span>-><span style="color: rgba(0, 0, 0, 1);">getTitle(); </span><span style="color: rgba(128, 0, 128, 1);">$taobao</span>-><span style="color: rgba(0, 0, 0, 1);">getTitle(); </span><span style="color: rgba(128, 0, 128, 1);">$google</span>-><span style="color: rgba(0, 0, 0, 1);">getTitle(); </span><span style="color: rgba(128, 0, 128, 1);">$runoob</span>-><span style="color: rgba(0, 0, 0, 1);">getUrl(); </span><span style="color: rgba(128, 0, 128, 1);">$taobao</span>-><span style="color: rgba(0, 0, 0, 1);">getUrl(); </span><span style="color: rgba(128, 0, 128, 1);">$google</span>-><span style="color: rgba(0, 0, 0, 1);">getUrl(); </span>?> |
结果为:
1 2 3 4 5 6 |
<span style="color: rgba(0, 0, 0, 1);">菜鸟教程 淘宝 Google 搜索 www</span>.runoob.<span style="color: rgba(0, 0, 0, 1);">com www</span>.taobao.<span style="color: rgba(0, 0, 0, 1);">com www</span>.google.com |
构造函数
用来在创建对象时初始化对象, 即为对象成员变量赋初始值,在创建对象的语句中与new运算符一起使用
语法:
void __construct ()
比如:
1 2 3 4 |
<span style="color: rgba(0, 0, 255, 1);">function</span> __construct( <span style="color: rgba(128, 0, 128, 1);">$par1</span>, <span style="color: rgba(128, 0, 128, 1);">$par2</span><span style="color: rgba(0, 0, 0, 1);"> ) { </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->url = <span style="color: rgba(128, 0, 128, 1);">$par1</span><span style="color: rgba(0, 0, 0, 1);">; </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->title = <span style="color: rgba(128, 0, 128, 1);">$par2</span><span style="color: rgba(0, 0, 0, 1);">; }</span> |
析构函数
与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数
语法:
void __destruct ( void )
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?<span style="color: rgba(0, 0, 0, 1);">php </span><span style="color: rgba(0, 0, 255, 1);">class</span><span style="color: rgba(0, 0, 0, 1);"> MyDestructableClass { </span><span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> __construct() { </span><span style="color: rgba(0, 0, 255, 1);">print</span> "构造函数n"<span style="color: rgba(0, 0, 0, 1);">; </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->name = "MyDestructableClass"<span style="color: rgba(0, 0, 0, 1);">; } </span><span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> __destruct() { </span><span style="color: rgba(0, 0, 255, 1);">print</span> "销毁 " . <span style="color: rgba(128, 0, 128, 1);">$this</span>->name . "n"<span style="color: rgba(0, 0, 0, 1);">; } } </span><span style="color: rgba(128, 0, 128, 1);">$obj</span> = <span style="color: rgba(0, 0, 255, 1);">new</span><span style="color: rgba(0, 0, 0, 1);"> MyDestructableClass(); </span>?> |
结果为:
1 2 |
<span style="color: rgba(0, 0, 0, 1);">构造函数 销毁 MyDestructableClass</span> |
继承
使用extends来继承一个类
语法:
class child extends parent {
…
}
方法重写
如果从父类继承的方法不能满足子类的需求,可以对其进行改写,这个过程叫方法的覆盖,也称为方法的重写
访问控制
php对属性或方法的访问控制,是通过在前面添加关键字来实现的
public:公有的类成员可以在任何地方被访问
protected:受保护的类成员则可以被其自身以及其子类和父类访问
private:私有的类成员则只能被其定义所在的类访问
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: rgba(0, 0, 255, 1);">class</span><span style="color: rgba(0, 0, 0, 1);"> MyClass { </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 声明一个公有的构造函数</span> <span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> __construct() { } </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 声明一个公有的方法</span> <span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> MyPublic() { } </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 声明一个受保护的方法</span> <span style="color: rgba(0, 0, 255, 1);">protected</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> MyProtected() { } </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 声明一个私有的方法</span> <span style="color: rgba(0, 0, 255, 1);">private</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> MyPrivate() { } } }</span> |
接口
接口通过interface关键字来定义,实现一个接口,使用implements操作符
接口中定义的所有方法都必须是公有,这是接口的特性
类中必须实现接口中定义的所有方法
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?<span style="color: rgba(0, 0, 0, 1);">php </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 声明一个'iTemplate'接口</span> <span style="color: rgba(0, 0, 255, 1);">interface</span><span style="color: rgba(0, 0, 0, 1);"> iTemplate { </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span> setVariable(<span style="color: rgba(128, 0, 128, 1);">$name</span>, <span style="color: rgba(128, 0, 128, 1);">$var</span><span style="color: rgba(0, 0, 0, 1);">); </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span> getHtml(<span style="color: rgba(128, 0, 128, 1);">$template</span><span style="color: rgba(0, 0, 0, 1);">); } </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 实现接口</span> <span style="color: rgba(0, 0, 255, 1);">class</span> Template <span style="color: rgba(0, 0, 255, 1);">implements</span><span style="color: rgba(0, 0, 0, 1);"> iTemplate { </span><span style="color: rgba(0, 0, 255, 1);">private</span> <span style="color: rgba(128, 0, 128, 1);">$vars</span> = <span style="color: rgba(0, 0, 255, 1);">array</span><span style="color: rgba(0, 0, 0, 1);">(); </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span> setVariable(<span style="color: rgba(128, 0, 128, 1);">$name</span>, <span style="color: rgba(128, 0, 128, 1);">$var</span><span style="color: rgba(0, 0, 0, 1);">) { </span><span style="color: rgba(128, 0, 128, 1);">$this</span>->vars[<span style="color: rgba(128, 0, 128, 1);">$name</span>] = <span style="color: rgba(128, 0, 128, 1);">$var</span><span style="color: rgba(0, 0, 0, 1);">; } </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span> getHtml(<span style="color: rgba(128, 0, 128, 1);">$template</span><span style="color: rgba(0, 0, 0, 1);">) { </span><span style="color: rgba(0, 0, 255, 1);">foreach</span>(<span style="color: rgba(128, 0, 128, 1);">$this</span>->vars <span style="color: rgba(0, 0, 255, 1);">as</span> <span style="color: rgba(128, 0, 128, 1);">$name</span> => <span style="color: rgba(128, 0, 128, 1);">$value</span><span style="color: rgba(0, 0, 0, 1);">) { </span><span style="color: rgba(128, 0, 128, 1);">$template</span> = <span style="color: rgba(0, 128, 128, 1);">str_replace</span>('{' . <span style="color: rgba(128, 0, 128, 1);">$name</span> . '}', <span style="color: rgba(128, 0, 128, 1);">$value</span>, <span style="color: rgba(128, 0, 128, 1);">$template</span><span style="color: rgba(0, 0, 0, 1);">); } </span><span style="color: rgba(0, 0, 255, 1);">return</span> <span style="color: rgba(128, 0, 128, 1);">$template</span><span style="color: rgba(0, 0, 0, 1);">; } }</span> |
常量
可以把在类中始终保持不变的值定义为常量。在定义和使用常量的时候不需要使用$符号
比如:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?<span style="color: rgba(0, 0, 0, 1);">php </span><span style="color: rgba(0, 0, 255, 1);">class</span><span style="color: rgba(0, 0, 0, 1);"> MyClass { </span><span style="color: rgba(0, 0, 255, 1);">const</span> <span style="color: rgba(0, 128, 128, 1);">constant</span> = '常量值'<span style="color: rgba(0, 0, 0, 1);">; </span><span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> showConstant() { </span><span style="color: rgba(0, 0, 255, 1);">echo</span> self::<span style="color: rgba(0, 128, 128, 1);">constant</span> . <span style="color: rgba(255, 0, 255, 1);">PHP_EOL</span><span style="color: rgba(0, 0, 0, 1);">; } } </span><span style="color: rgba(0, 0, 255, 1);">echo</span> MyClass::<span style="color: rgba(0, 128, 128, 1);">constant</span> . <span style="color: rgba(255, 0, 255, 1);">PHP_EOL</span>; <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> ::代表调用类中的静态方法或者常量,属性</span> ?> |
抽象类
任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的
定义为抽象的类不能被实例化
被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现
子类方法可以包含父类抽象方法中不存在的可选参数
比如:
1 2 3 4 5 6 7 8 9 10 11 |
<span style="color: rgba(0, 0, 255, 1);">abstract</span> <span style="color: rgba(0, 0, 255, 1);">class</span><span style="color: rgba(0, 0, 0, 1);"> AbstractClass { </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 强制要求子类定义这些方法</span> <span style="color: rgba(0, 0, 255, 1);">abstract</span> <span style="color: rgba(0, 0, 255, 1);">protected</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> getValue(); </span><span style="color: rgba(0, 0, 255, 1);">abstract</span> <span style="color: rgba(0, 0, 255, 1);">protected</span> <span style="color: rgba(0, 0, 255, 1);">function</span> prefixValue(<span style="color: rgba(128, 0, 128, 1);">$prefix</span><span style="color: rgba(0, 0, 0, 1);">); </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);"> 普通方法(非抽象方法)</span> <span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">function</span><span style="color: rgba(0, 0, 0, 1);"> printOut() { </span><span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(128, 0, 128, 1);">$this</span>->getValue() . <span style="color: rgba(255, 0, 255, 1);">PHP_EOL</span><span style="color: rgba(0, 0, 0, 1);">; } }</span> |
static关键字
声明类属性或方法为static,就可以不实例化类而直接访问
静态属性不能通过一个类已实例化的对象来访问(但静态方法可以)
由于静态方法不需要通过对象即可调用,所以伪变量$this在静态方法中不可用
静态属性不可以由对象通过->操作符来访问
final关键字
如果父类中的方法被声明为final,则子类无法覆盖该方法。如果一个类被声明为final,则不能被继承
php不会在子类的构造方法中自动的调用父类的构造方法。要执行父类的构造方法,需要在子类的构造方法中调用parent::__construct()