Jadual Kandungan
PHP – 面向对象
Table of Contents
函数调用
有 __clone 和无 __clone
继承
protected
static
final
const
后期绑定, 又叫延迟绑定
单例模式 instanceof
魔术方法
重载 覆盖
魔术常量
抽象类和接口
命名空间
其他对象方法
自动加载

PHP - 面向对象

Jun 13, 2016 pm 12:23 PM
class echo function public test

PHP -- 面向对象

PHP – 面向对象

PHP – 面向对象

Table of Contents

  • 函数调用
  • 有 __clone 和无 __clone
  • 继承
  • protected
  • static
  • final
  • const
  • 后期绑定, 又叫延迟绑定
  • 单例模式 instanceof
  • 魔术方法
  • 重载 覆盖
  • 魔术常量
  • 抽象类和接口
  • 命名空间
  • 其他对象方法
  • 自动加载
  • TODO SPL 函数

函数调用

<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f1</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'you call me!'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f2</span>() {        f1();    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #ffffff; background-color: #000000;">f2</span>();                  <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#25253;&#38169;</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">public function f2() {  // &#27491;&#30830;</span><span style="color: #ff7f24;">//     </span><span style="color: #ff7f24;">self::f1();</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">}</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">public function f2() {  // &#27491;&#30830;</span><span style="color: #ff7f24;">//     </span><span style="color: #ff7f24;">static::f1();</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">}</span>
Salin selepas log masuk

有 __clone 和无 __clone

一个值得注意的地方 __clone() cannot accept any arguments
Salin selepas log masuk
  1. 直接赋值 (复制), 不会产生新的对象
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__construct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'ok'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__destruct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'fail'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span>;               <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#25351;&#21521;&#21516;&#19968;&#20010;&#20869;&#23384;&#31354;&#38388;</span><span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'<hr>'</span>;<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">ok</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">-----</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">fail</span>
    Salin selepas log masuk

  2. clone 会生成一个新的对象, 但不会调用构造函数
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__construct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'ok'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__destruct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'fail'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #00ffff;">clone</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span>;         <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#25351;&#21521;&#19981;&#21516;&#30340; &#20869;&#23384;&#31354;&#38388;</span><span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'<hr>'</span>;<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">ok</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">---------------</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">fail</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">fail</span>
    Salin selepas log masuk

  3. __clone 调用析构函数
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__construct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'ok'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__destruct</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'fail'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__clone</span>() {        <span style="color: #00ffff;">return</span> <span style="color: #00ffff;">self</span>::__construct();    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #00ffff;">clone</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span>;         <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#25351;&#21521;&#19981;&#21516;&#30340; &#20869;&#23384;&#31354;&#38388;</span><span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'<hr>'</span>;<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">ok</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">ok</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">-----</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">fail</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">fail</span>
    Salin selepas log masuk

继承

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">class</span> <span style="color: #98fb98;">A</span> {    <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">foo</span>() {        <span style="color: #00ffff;">if</span> (<span style="color: #00ffff;">isset</span>(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>)) {            <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'this is defined ('</span>,  get_class(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>), <span style="color: #ffa07a;">')'</span>, <span style="color: #ffa07a;">'<br>'</span>;        } <span style="color: #00ffff;">else</span> {            <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'this is not defined'</span>, <span style="color: #ffa07a;">'<br>'</span>;        }    }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">B</span> {    <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">bar</span>() {        <span style="color: #7fffd4;">A</span>::foo();    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">A</span>();                   <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this &#21464;&#25104; A</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span>-><span style="color: #ffffff; background-color: #000000;">foo</span>();<span style="color: #7fffd4;">A</span>::foo();<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is defined (A)</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is not defined</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">B</span>();                   <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this &#21464;&#25104; B</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span>-><span style="color: #ffffff; background-color: #000000;">bar</span>();<span style="color: #7fffd4;">B</span>::bar();<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is defined (B)</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is not defined</span>
Salin selepas log masuk

继承的时候权限只能越来越大, 不能变小

<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">A</span> {    <span style="color: #00ffff;">protected</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #ffa07a;">'a'</span>;    <span style="color: #00ffff;">protected</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>-><span style="color: #eedd82;">a</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">B</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">A</span>{    <span style="color: #00ffff;">private</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>-><span style="color: #eedd82;">a</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">B</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span>-><span style="color: #ffffff; background-color: #000000;">f</span>();                  <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#25253;&#38169;</span>
Salin selepas log masuk

protected

<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">A</span> {    <span style="color: #00ffff;">protected</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #ffa07a;">'a'</span>;    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>-><span style="color: #eedd82;">a</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">B</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">A</span>{    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>-><span style="color: #eedd82;">a</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">B</span>();                   <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#21487;&#20197;&#32487;&#25215; protected</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span>-><span style="color: #ffffff; background-color: #000000;">f</span>();
Salin selepas log masuk

static

static 变量
所有实例化的实例化的类共享的变量, 其中一个改变了, 其他的也跟着改变

class stu {    public static $fee;    function f($fee) {        self::$fee += $fee;    }}
Salin selepas log masuk

static 方法
不可以操作 $this 变量, 使用 static 方法的情景, 一般的原则是某个方法中不包含 $this 变量

calss stu {    static function func1() {        static::func2();    }    static function func2() {         ...    }}
Salin selepas log masuk

调用: stu::func2();, stu::func1();

final

使用 finnal 的场景:

  1. 出于安全的考虑, 某个类的方法不允许被修改
  2. 不希望被其他的类继承

final 不能修饰成员属性 (变量)

final 方法, 表示此方法不能被重写

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">class</span> <span style="color: #98fb98;">BaseClass</span> {   <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">test</span>() {       <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">"BaseClass::test() called\n"</span>;   }   <span style="color: #00ffff;">final</span> <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">moreTesting</span>() {       <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">"BaseClass::moreTesting() called\n"</span>;   }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">ChildClass</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">BaseClass</span> {   <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">moreTesting</span>() {       <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">"ChildClass::moreTesting() called\n"</span>;   }}<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">Results in Fatal error: Cannot override final method BaseClass::moreTesting()</span>
Salin selepas log masuk

final 类, 表示此类不能被继续被 extends

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">final</span> <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">BaseClass</span> {   <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">test</span>() {       <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">"BaseClass::test() called\n"</span>;   }   <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#36825;&#37324;&#26080;&#35770;&#20320;&#26159;&#21542;&#23558;&#26041;&#27861;&#22768;&#26126;&#20026; final&#65292;&#37117;&#27809;&#26377;&#20851;&#31995;</span>   <span style="color: #00ffff;">final</span> <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">moreTesting</span>() {       <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">"BaseClass::moreTesting() called\n"</span>;   }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">ChildClass</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">BaseClass</span> {}<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#20135;&#29983; Fatal error: Class ChildClass may not inherit from final class (BaseClass)</span>
Salin selepas log masuk

const

<span style="color: #00ffff;">class</span>  <span style="color: #98fb98;">MyClass</span> {    <span style="color: #00ffff;">const</span>  <span style="color: #98fb98;">constant</span>  =  <span style="color: #ffa07a;">'constant value'</span> ;    <span style="color: #00ffff;">function</span>  <span style="color: #87cefa;">showConstant</span> () {        <span style="color: #00ffff;">echo</span>   <span style="color: #00ffff;">self</span> :: constant  .  <span style="color: #ffa07a;">"\n"</span> ;    }}<span style="color: #00ffff;">echo</span>  <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">class</span> :: constant . <span style="color: #ffa07a;">"\n"</span> ;  <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#33258; PHP 5.3.0 &#36215;</span>
Salin selepas log masuk

后期绑定, 又叫延迟绑定

延迟绑定的意思, self 的绑定是运行时候计算, 看下面

<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">Par</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">whoami</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'parent'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">say</span>() {        <span style="color: #00ffff;">self</span>::whoami();    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">say2</span>() {        <span style="color: #00ffff;">static</span>::whoami();    }}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">Sun</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">Par</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">whoami</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'sun'</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}<span style="color: #7fffd4;">Sun</span>::say();                     <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">parent, &#22240;&#20026;&#25191;&#34892;&#29615;&#22659;&#21464;&#25104;&#20102; parent</span><span style="color: #7fffd4;">Sun</span>::say2();                    <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">sun, &#20445;&#25345;&#38745;&#24577;</span>
Salin selepas log masuk

单例模式 instanceof

<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">mysql</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">ins</span>;    <span style="color: #00ffff;">private</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__construct</span>() {        <span style="color: #ff7f24;">/* </span><span style="color: #ff7f24;">do something */</span>    }    <span style="color: #00ffff;">private</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__clone</span> () {}      <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#31105;&#27490;&#32487;&#25215;&#30340;&#31867;&#20462;&#25913;&#33258;&#36523;, &#20063;&#21487;&#20197;&#20351;&#29992; final</span>    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">static</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">getIns</span>() {        <span style="color: #00ffff;">if</span> (!(<span style="color: #00ffff;">self</span>::<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">ins</span> <span style="color: #00ffff;">instanceof</span> <span style="color: #98fb98;">mysql</span>)) {            <span style="color: #00ffff;">self</span>::<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">ins</span> = <span style="color: #00ffff;">new</span> <span style="color: #00ffff;">self</span>();        }        <span style="color: #00ffff;">return</span> <span style="color: #00ffff;">self</span>::<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">ins</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> = <span style="color: #7fffd4;">mysql</span>::getIns();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span> = <span style="color: #7fffd4;">mysql</span>::getIns();var_dump (<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">a</span> === <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">b</span>);              <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">true</span>
Salin selepas log masuk

魔术方法

  1. __get
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">hello</span> = <span style="color: #ffa07a;">'normal hello'</span>;    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__get</span>(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span>) {        <span style="color: #00ffff;">if</span> (<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span> == <span style="color: #ffa07a;">'hello'</span>) {            <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'from get: hello'</span>;        } <span style="color: #00ffff;">else</span> <span style="color: #00ffff;">if</span> (<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span> = <span style="color: #ffa07a;">'other'</span>) {            <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'from get: other'</span>;        }    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #eedd82;">hello</span>, <span style="color: #ffa07a;">'<br>'</span>;       <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">normal hello, &#22914;&#26524;&#23384;&#22312;&#19981;&#20250;&#35843;&#29992; &#39764;&#26415;&#26041;&#27861;</span><span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #eedd82;">other</span>;               <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#19981;&#33021;&#35843;&#29992;, &#35843;&#29992;&#39764;&#26415;&#26041;&#27861;</span>
    Salin selepas log masuk

  2. __set
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">hello</span> = <span style="color: #ffa07a;">'normal hello'</span>;    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__set</span>(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">k</span>, <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">v</span>) {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'you want to set me! '</span>, <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">k</span>, <span style="color: #ffa07a;">'<br>'</span>;        <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #7fffd4;">this</span>-><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">k</span> = <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">v</span>;    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #eedd82;">ttt</span> = 222;<span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #eedd82;">ttt</span>;          <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#21487;&#20197;&#35774;&#32622;</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #eedd82;">hello</span> = <span style="color: #ffa07a;">'another hello'</span>;print_r(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>);<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">you want to set me! ttt</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">222</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">test Object</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">(</span><span style="color: #ff7f24;">//   </span><span style="color: #ff7f24;">[hello] => another hello</span><span style="color: #ff7f24;">//   </span><span style="color: #ff7f24;">[ttt] => 222</span><span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">)</span>
    Salin selepas log masuk

  3. __isset
    class test {    public function __isset($v) {        return false;        .....        return true;    }}
    Salin selepas log masuk
    $t = new test();if (isset($t->value)) {    echo 'isset';} else {    echo 'noset';}
    Salin selepas log masuk
  4. __unset
    调用的属性不存在, 就会调用此函数, 主要用于友好操作
  5. __call
    无权调用的时候, 传递两个参数, (function_NAME, array function_ARGS)
    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">func_empty</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'nothing here'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__call</span> (<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">func</span>, <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span>) {        <span style="color: #00ffff;">self</span>::func_empty();    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();
    Salin selepas log masuk

    <span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">func_empty</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'nothing here'</span>;    }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">__call</span> (<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">func</span>, <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span>) {        print_r(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">args</span>);        <span style="color: #00ffff;">self</span>::func_empty();    }}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t</span>-><span style="color: #ffffff; background-color: #000000;">abc</span>(<span style="color: #ffa07a;">'abc'</span>, <span style="color: #ffa07a;">'def'</span>);<span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">Array ( [0] => abc [1] => def )</span>
    Salin selepas log masuk

  6. __callStatic
    public static function  __callStatic ( $name ,  $arguments ) { ... }
    Salin selepas log masuk

重载 覆盖

覆盖: override 指的是子类覆盖父类的方法, 在 PHP 中, 如果 子类和父类的参数不一样会有警告
重载: redeclare 指的是同一个类中包含两个相同名字的方法, PHP 不支持

实现重载的方法

func_num_args()           返回数据的参数的个数$arr = func_get_args()    让参数以数组的形式返回func_get_arg(0)           返回第一个参数func_get_arg(1)           返回第二个参数
Salin selepas log masuk
<span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {    <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">arr</span> = func_get_args();    <span style="color: #00ffff;">foreach</span>(<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">arr</span> <span style="color: #00ffff;">as</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">v</span>) {        ....   }}
Salin selepas log masuk

魔术常量

__LINE____FILE____DIR____FUNCTION____CLASS____METHOD__
Salin selepas log masuk

抽象类和接口

abstract

abstract class Car {   abstract function func1();}class Fastcar extends Car {   public function func1() {     ...   }}
Salin selepas log masuk
  1. 继承类必须实现抽象类的 所有 方法
  2. 抽象类可以有自己的属性
  3. 抽象类可以有自己的实现方法, 也叫具体方法, interface 没有
  4. 可以有 protected 方法, interface 不可以

interface

<span style="color: #00ffff;">interface</span> <span style="color: #98fb98;">iUsb</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>();}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">phone</span> <span style="color: #00ffff;">implements</span> <span style="color: #98fb98;">iUsb</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">f</span>() {        ..    }}
Salin selepas log masuk

定义规范

当多个类, 之间平级的, 有相同的方法, 但是实现不同
Salin selepas log masuk

类可以实现多个接口, 接口可以实现多个接口

class t1 implements i1, i2, i3 { }interface i1 extends i2, i2, i3 {}
Salin selepas log masuk
  1. 继承类要实现 接口的全部方法
  2. interface 不能有 private 的方法
  3. interface 里面的属性只能是 常量
  4. interface 里面不能实现方法, 抽象类可以, (可以理解接口里面全都是抽象方法)
  5. interface 可以定义常量
<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">Monkey</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">climb</span>() { ... }}<span style="color: #00ffff;">interface</span> <span style="color: #98fb98;">iBirdable</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">fly</span>();}<span style="color: #00ffff;">interface</span> <span style="color: #98fb98;">iFishable</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">swim</span>();}<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">NewMonkey</span> <span style="color: #00ffff;">extends</span> <span style="color: #98fb98;">Monkey</span> <span style="color: #00ffff;">implements</span> <span style="color: #98fb98;">iBirdable</span>, <span style="color: #98fb98;">iFishable</span> {    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">fly</span>()  { ... }    <span style="color: #00ffff;">public</span> <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">swim</span>() { ... }}
Salin selepas log masuk

抽象类和接口的区别我不是 很熟悉 T_T, mark 个 todo 以后再来补充

  1. 如果要创建一个模型, 这个模型将由一些紧密的对象采用, 就可以使用抽象类. 如果要创建由一些不相关对象采用的功能, 使用接口
  2. 如果必须从多个来源继承行为, 就使用接口. 接口可以实现多个, 不可以继承多个抽象类
  3. 如果所有的类会有共享一个公共的行为实现, 就使用抽象类, 并在其中的实现该行为, 在接口中无法实现具体方法

命名空间

如果文件 1 有 test 对象, 文件 2 也有 test 对象
在文件 3

require 'file1.php';require 'file2.php';
Salin selepas log masuk

那么就会出现冲突

test1.php

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">namespace</span> <span style="color: #98fb98;">test\file1</span>;<span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">val</span> = <span style="color: #ffa07a;">'this is file 1'</span>;}
Salin selepas log masuk

test2.php

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">namespace</span> <span style="color: #98fb98;">test\file2</span>;      <span style="color: #ff7f24;"># &#20063;&#21487;&#20197;&#20351;&#29992;&#21035;&#30340;&#21629;&#21517;&#31354;&#38388; ...\...\...\...</span><span style="color: #00ffff;">class</span> <span style="color: #98fb98;">test</span> {    <span style="color: #00ffff;">public</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">val</span> = <span style="color: #ffa07a;">'this is file 2'</span>;}
Salin selepas log masuk

test.php 用于加载访问

<span style="color: #b0c4de;"><?php</span><span style="color: #00ffff;">require</span> <span style="color: #ffa07a;">'test1.php'</span>;<span style="color: #00ffff;">require</span> <span style="color: #ffa07a;">'test2.php'</span>;<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t1</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test\file1\test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t2</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">test\file2\test</span>();<span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t1</span>-><span style="color: #eedd82;">val</span>, <span style="color: #ffa07a;">'<br>'</span>;        <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is file1</span><span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t2</span>-><span style="color: #eedd82;">val</span>, <span style="color: #ffa07a;">'<br>'</span>;        <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is file2</span><span style="color: #00ffff;">use</span> <span style="color: #98fb98;">test\file1</span> <span style="color: #00ffff;">as</span> <span style="color: #98fb98;">file1</span>;<span style="color: #00ffff;">use</span> <span style="color: #98fb98;">test\file2</span> <span style="color: #00ffff;">as</span> <span style="color: #98fb98;">file2</span>;<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t3</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">file1\test</span>();<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t4</span> = <span style="color: #00ffff;">new</span> <span style="color: #98fb98;">file2\test</span>();<span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t3</span>-><span style="color: #eedd82;">val</span>, <span style="color: #ffa07a;">'<br>'</span>;        <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is file1</span><span style="color: #00ffff;">echo</span> <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">t4</span>-><span style="color: #eedd82;">val</span>, <span style="color: #ffa07a;">'<br>'</span>;        <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">this is file2</span>
Salin selepas log masuk

其他对象方法

class_alias(class_name, new_classname)get_class (OBJ)$arr = get_declared_classes()       # 返回已经定义了的类get_parent_class (OBJ)interface_exists (INTER)            # 是否存在接口method_exists (OBJ, FUNC_NAME)      # 是否存在某个方法
Salin selepas log masuk

自动加载

<span style="color: #00ffff;">function</span>  <span style="color: #87cefa;">__autoload</span> ( <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">class_name</span> ) {    <span style="color: #00ffff;">require</span>  <span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">class_name</span>  .  <span style="color: #ffa07a;">'.php'</span> ;}<span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">obj</span>   = <span style="color: #00ffff;">new</span>  <span style="color: #98fb98;">MyClass1</span> ();      <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#33258;&#21160;&#20250;&#35843;&#29992;&#19978;&#38754;&#30340;&#21738;&#20010;&#20989;&#25968;, &#21442;&#25968;&#21517;&#26159; MyClass1</span><span style="color: #ffffff; background-color: #000000;">$</span><span style="color: #eedd82;">obj2</span>  = <span style="color: #00ffff;">new</span>  <span style="color: #98fb98;">MyClass2</span> ();      <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#33258;&#21160;&#20250;&#35843;&#29992;&#19978;&#38754;&#30340;&#21738;&#20010;&#20989;&#25968;, &#21442;&#25968;&#21517;&#26159; MyClass2</span>
Salin selepas log masuk

为什么函数里面的 require class 的生活周期可以到外层, 原因像下面的,
__autoload 被执行之后, 就相当于把函数里面的代码展示出来

<span style="color: #00ffff;">function</span> <span style="color: #87cefa;">well</span>() {    <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">test</span>() {        <span style="color: #00ffff;">echo</span> <span style="color: #ffa07a;">'inside '</span>, <span style="color: #ffa07a;">'<br>'</span>;    }}well();      <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">&#19968;&#23450;&#35201;&#20808;&#35843;&#29992;</span>test();      <span style="color: #ff7f24;">// </span><span style="color: #ff7f24;">inside</span>
Salin selepas log masuk

__autoload 的实现spl_autoload_register —— 注册给定的函数作为 __autoload 的实现
Salin selepas log masuk
<span style="color: #00ffff;">function</span> <span style="color: #87cefa;">func</span>() {    <span style="color: #00ffff;">require</span> <span style="color: #ffa07a;">'...'</span>;}spl_autoload_register(func);
Salin selepas log masuk

TODO SPL 函数

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Jam pintar Huawei Watch GT 5 mendapat kemas kini dengan ciri baharu Jam pintar Huawei Watch GT 5 mendapat kemas kini dengan ciri baharu Oct 03, 2024 am 06:25 AM

Huawei sedang melancarkan perisian versi 5.0.0.100(C00M01) untuk Watch GT 5 dan Watch GT 5 Prosmartwatches di seluruh dunia. Kedua-dua jam tangan pintar ini baru-baru ini dilancarkan di Eropah, dengan model standard tiba sebagai model termurah syarikat. Harmoni ini

Mimpi Kolonel Sanders Tekken digoreng oleh KFC Mimpi Kolonel Sanders Tekken digoreng oleh KFC Oct 02, 2024 am 06:07 AM

Katsuhiro Harada, pengarah siri Tekken, pernah cuba membawa Kolonel Sanders ke dalam permainan pertempuran ikonik. Dalam temu bual dengan TheGamer, Harada mendedahkan bahawa dia mengemukakan idea itu kepada KFC Jepun, dengan harapan dapat menambah legenda makanan segera itu sebagai

Ulasan Cybertruck FSD memuji penukaran lorong cepat dan visualisasi skrin penuh Ulasan Cybertruck FSD memuji penukaran lorong cepat dan visualisasi skrin penuh Oct 01, 2024 am 06:16 AM

Tesla melancarkan versi Pemanduan Sendiri Penuh (Diawasi) terbaharu 12.5.5 dan dengannya disertakan pilihan Cybertruck FSD yang dijanjikan pada akhirnya, sepuluh bulan selepas pikap mula dijual dengan ciri yang disertakan dalam harga kemasan Siri Asas. F

Pandangan pertama: Video pembongkaran kotak yang bocor bagi pengecas dinding 140W 4-port Anker Zolo akan datang dengan paparan Pandangan pertama: Video pembongkaran kotak yang bocor bagi pengecas dinding 140W 4-port Anker Zolo akan datang dengan paparan Oct 01, 2024 am 06:32 AM

Terdahulu pada September 2024, pengecas Zolo 140W Anker telah bocor, dan ia adalah masalah besar kerana ia merupakan pengecas dinding pertama dengan paparan daripada syarikat itu. Kini, video unboxing baharu daripada Xiao Li TV di YouTube memberi kami pandangan pertama tentang hi

Garmin mengeluarkan peningkatan aktiviti Adventure Racing untuk berbilang jam tangan pintar melalui kemas kini baharu Garmin mengeluarkan peningkatan aktiviti Adventure Racing untuk berbilang jam tangan pintar melalui kemas kini baharu Oct 01, 2024 am 06:40 AM

Garmin mengakhiri bulan ini dengan set kemas kini stabil baharu untuk jam tangan pintar mewah terbaharunya. Untuk mengimbas kembali, syarikat itu mengeluarkan Perisian Sistem 11.64 untuk memerangi kehabisan bateri yang tinggi merentasi Enduro 3, Fenix ​​E dan Fenix ​​8 (sekira $1,099.99 di Amazon).

Pemanas Minyak Xiaomi Mijia Graphene baharu dengan HyperOS tiba Pemanas Minyak Xiaomi Mijia Graphene baharu dengan HyperOS tiba Oct 02, 2024 pm 09:02 PM

Xiaomi akan melancarkan Pemanas Minyak Mijia Graphene sebentar lagi di China. Syarikat itu baru-baru ini menjalankan kempen crowdfunding yang berjaya untuk produk rumah pintar, dihoskan pada platform Youpinnya. Menurut halaman tersebut, peranti telah mula dihantar ke

Samsung Galaxy Z Fold Edisi Khas didedahkan untuk mendarat pada akhir Oktober apabila nama yang bercanggah muncul Samsung Galaxy Z Fold Edisi Khas didedahkan untuk mendarat pada akhir Oktober apabila nama yang bercanggah muncul Oct 01, 2024 am 06:21 AM

Pelancaran peranti lipat 'Edisi Khas' Samsung yang telah lama ditunggu-tunggu telah mengambil satu kelainan lagi. Dalam beberapa minggu kebelakangan ini, khabar angin mengenai apa yang dipanggil Galaxy Z Fold Edisi Khas menjadi agak senyap. Sebaliknya, tumpuan telah beralih kepada siri Galaxy S25, termasuk

Manjaro 24.1 \'Xahea\' dilancarkan dengan KDE Plasma 6.1.5, VirtualBox 7.1 dan banyak lagi Manjaro 24.1 \'Xahea\' dilancarkan dengan KDE Plasma 6.1.5, VirtualBox 7.1 dan banyak lagi Oct 02, 2024 am 06:06 AM

Dengan sejarah lebih satu dekad, Manjaro dianggap sebagai salah satu distro Linux yang paling mesra pengguna yang sesuai untuk pemula dan pengguna berkuasa, kerana mudah dipasang dan digunakan. Kebanyakannya dibangunkan di Austria, Jerman dan Perancis, distro berasaskan Arch ini

See all articles