Home  >  Article  >  Backend Development  >  Object-oriented PHP (4)

Object-oriented PHP (4)

WBOY
WBOYOriginal
2016-07-29 09:16:15928browse

<code><span><span><?php</span><span>/**
*   接口就是把不同类的共同行为进行了定义,然后在不同的类里面实现不同的功能。
*   因为接口的方法实现可以有很多,所以对于接口里面定义的方法的具体实现是多种多样的,这种特性我们称为多态
*/</span>
header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span><span>//interface关键字用于定义接口</span><span><span>interface</span><span>ICanEat</span>{</span><span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>;</span>
    }
    <span>//implements关键字用于表示类实现某个接口</span><span><span>class</span><span>Human</span><span>implements</span><span>ICanEat</span>
    {</span><span>//实现某个接口后,必须提供接口中定义的方法的具体实现</span><span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>{</span><span>echo</span><span>"Human::eat()"</span>.<span>$food</span>.<span>"<br/>"</span>;
        }
    }

    <span><span>class</span><span>Animal</span><span>implements</span><span>ICanEat</span>
    {</span><span>//实现某个接口后,必须提供接口中定义的方法的具体实现</span><span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>{</span><span>echo</span><span>"Animal::eat()"</span>.<span>$food</span>.<span>"<br/>"</span>;
        }
    }

    <span>$pzy</span>=<span>new</span> Human();
    <span>$pzy</span>->eat(<span>"大西瓜"</span>);

    <span>$dog</span>=<span>new</span> Animal();
    <span>$dog</span>->eat(<span>"大骨头"</span>);
    <span>//instanceof用于判断某个对象是否实现了某个接口</span>
    var_dump(<span>$dog</span><span>instanceof</span> ICanEat);
    <span><span>function</span><span>checkEat</span><span>(<span>$obj</span>)</span>{</span><span>if</span> (<span>$obj</span><span>instanceof</span> ICanEat) {
            <span>$obj</span>->eat(<span>'food'</span>);
        }<span>else</span>{
            <span>echo</span><span>"The obj can't eat.<br/>"</span>;
        }
    }
<span>//相同的一行代码,对于传入不同的接口的实现的对象的时候,表现不同的,称为多态</span>
checkEat(<span>$pzy</span>);
checkEat(<span>$dog</span>);


<span>//用extends让接口继承接口</span><span><span>interface</span><span>ICanFly</span><span>extends</span><span>ICanEat</span>{</span><span>public</span><span><span>function</span><span>fly</span><span>()</span>;</span>
}
<span>//当类实现子接口时,父接口定义的方法也要在这个类中具体实现</span><span><span>class</span><span>Human1</span><span>implements</span><span>ICanFly</span>
{</span><span>public</span><span><span>function</span><span>fly</span><span>()</span>{</span>}
    <span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>{</span>}
}
<span>?></span></span></code>

Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above introduces object-oriented PHP (IV), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn