Rumah  >  Artikel  >  pembangunan bahagian belakang  >  移动app接口编程技术-学习实现之PHP类其他特性

移动app接口编程技术-学习实现之PHP类其他特性

WBOY
WBOYasal
2016-07-30 13:31:23999semak imbas

  • Static静态关键字

    静态。 在类中,被他标记的变量或方法,不属于任何一个对象。访问的时候要使用“::“。并且在类中自我调用的时候要是用”self::”
    例如:

<code><span><span><?php </span><span><span>class</span><span>Car</span> {</span><span>private</span><span>static</span><span>$speed</span> = <span>10</span>;

    <span>public</span><span><span>function</span><span>getSpeed</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$speed</span>;
    }

    <span>//在这里定义一个静态方法,实现速度累加10</span><span>public</span><span>static</span><span><span>function</span><span>speedUp</span><span>()</span>
    {</span><span>return</span><span>self</span>::<span>$speed</span> += <span>10</span>;
    }
}

<span>$car</span> = <span>new</span> Car();
Car::speedUp();  <span>//调用静态方法加速</span><span>echo</span><span>$car</span>->getSpeed();  <span>//调用共有方法输出当前的速度值</span></span></span></code>

静态方法也可以通过变量来进行动态调用。

<code><span>$func</span> = <span>'getSpeed'</span>;
<span>$className</span> = <span>'Car'</span>;
<span>echo</span><span>$className</span>::<span>$func</span>();  <span>//动态调用静态方法</span></code>
  • 访问控制

访问控制通过关键字public,protected和private来实现。被定义为公有的类成员可以在任何地方被访问。被定义为受保护的类成员则可以被其自身以及其子类和父类访问。被定义为私有的类成员则只能被其定义所在的类访问。

类属性必须定义为公有、受保护、私有之一。

类中的方法可以被定义为公有、私有或受保护。如果没有设置这些关键字,则该方法默认为公有。

如果构造函数定义成了私有方法,则不允许直接实例化对象了,这时候一般通过静态方法进行实例化,在设计模式中会经常使用这样的方法来控制对象的创建,比如单例模式只允许有一个全局唯一的对象。

<code><span><span>class</span><span>Car</span> {</span><span>private</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>echo</span><span>'object create'</span>;
    }

    <span>private</span><span>static</span><span>$_object</span> = <span>null</span>;
    <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>if</span> (<span>empty</span>(<span>self</span>::<span>$_object</span>)) {
            <span>self</span>::<span>$_object</span> = <span>new</span> Car(); 
            <span>//内部方法可以调用私有方法,因此这里可以创建对象</span>
        }
        <span>return</span><span>self</span>::<span>$_object</span>;
    }
}
<span>//$car = new Car(); //这里不允许直接实例化对象</span><span>$car</span> = Car::getInstance(); <span>//通过静态方法来获得一个实例</span></code>
  • 继承
<code><span><span><?php </span><span><span>class</span><span>Car</span> {</span><span>public</span><span>$speed</span> = <span>0</span>; <span>//汽车的起始速度是0</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed += <span>10</span>;
        <span>return</span><span>$this</span>->speed;
    }
}
<span>//定义继承于Car的Truck类</span><span><span>class</span><span>Truck</span><span>extends</span><span>Car</span>{</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed = <span>parent</span>::speedUp() + <span>50</span>;
    }
}

<span>$car</span> = <span>new</span> Truck();
<span>$car</span>->speedUp();
<span>echo</span><span>$car</span>->speed;</span></span></code>
  • 重载

PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的。属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值、读取、判断属性是否设置、销毁属性。

<code><span><span>class</span><span>Car</span> {</span><span>private</span><span>$ary</span> = <span>array</span>();

    <span>public</span><span><span>function</span><span>__set</span><span>(<span>$key</span>, <span>$val</span>)</span> {</span><span>$this</span>->ary[<span>$key</span>] = <span>$val</span>;
    }

    <span>public</span><span><span>function</span><span>__get</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) {
            <span>return</span><span>$this</span>->ary[<span>$key</span>];
        }
        <span>return</span><span>null</span>;
    }

    <span>public</span><span><span>function</span><span>__isset</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) {
            <span>return</span><span>true</span>;
        }
        <span>return</span><span>false</span>;
    }

    <span>public</span><span><span>function</span><span>__unset</span><span>(<span>$key</span>)</span> {</span><span>unset</span>(<span>$this</span>->ary[<span>$key</span>]);
    }
}
<span>$car</span> = <span>new</span> Car();
<span>$car</span>->name = <span>'汽车'</span>;  <span>//name属性动态创建并赋值</span><span>echo</span><span>$car</span>->name;
</code>

方法的重载通过__call来实现,当调用不存在的方法的时候,将会转为参数调用__call方法,当调用不存在的静态方法时会使用__callStatic重载。

<code>lass Car {
    <span>public</span><span>$speed</span> = <span>0</span>;

    <span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span>, <span>$args</span>)</span> {</span><span>if</span> (<span>$name</span> == <span>'speedUp'</span>) {
            <span>$this</span>->speed += <span>10</span>;
        }
    }
}
<span>$car</span> = <span>new</span> Car();
<span>$car</span>->speedUp(); <span>//调用不存在的方法会使用重载</span><span>echo</span><span>$car</span>->speed;</code>
  • 类对象比较

对象比较,当同一个类的两个实例的所有属性都相等时,可以使用比较运算符“==“进行判断,当需要判断两个变量是否为同一个对象的引用时,可以使用全等运算符“===“进行判断。

<code><span><span>class</span><span>Car</span> {</span>
}
<span>$a</span> = <span>new</span> Car();
<span>$b</span> = <span>new</span> Car();
<span>if</span> (<span>$a</span> == <span>$b</span>) <span>echo</span><span>'=='</span>;   <span>//true</span><span>if</span> (<span>$a</span> === <span>$b</span>) <span>echo</span><span>'==='</span>; <span>//false</span></code>

对象复制,在一些特殊情况下,可以通过关键字clone来复制一个对象,这时__clone方法会被调用,通过这个魔术方法来设置属性的值。

<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>;

    <span>public</span><span><span>function</span><span>__clone</span><span>()</span> {</span><span>$obj</span> = <span>new</span> Car();
        <span>$obj</span>->name = <span>$this</span>->name;
    }
}
<span>$a</span> = <span>new</span> Car();
<span>$a</span>->name = <span>'new car'</span>;
<span>$b</span> = <span>clone</span><span>$a</span>;
var_dump(<span>$b</span>);
</code>

对象序列化,可以通过serialize方法将对象序列化为字符串,用于存储或者传递数据,然后在需要的时候通过unserialize将字符串反序列化成对象进行使用。

<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>;
}
<span>$a</span> = <span>new</span> Car();
<span>$str</span> = serialize(<span>$a</span>); <span>//对象序列化成字符串</span><span>echo</span><span>$str</span>.<span>'<br>'</span>;
<span>$b</span> = unserialize(<span>$str</span>); <span>//反序列化为对象</span>
var_dump(<span>$b</span>);
</code>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了移动app接口编程技术-学习实现之PHP类其他特性,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
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