Maison > développement back-end > tutoriel php > 36 PHP面向对象

36 PHP面向对象

WBOY
Libérer: 2016-07-29 09:15:22
original
1049 Les gens l'ont consulté

1. __set() 和 __get() 方法

<code><span><span>class</span><span>A</span>
    {</span><span>private</span><span>$n1</span>;
        <span>private</span><span>$n2</span>;
        <span>private</span><span>$n3</span>;

        <span>//使用__set()方法来管理所有的属性</span><span>public</span><span><span>function</span><span>__set</span><span>(<span>$pro_name</span>,<span>$pro_val</span>)</span>
        {</span><span>$this</span>->pro_name = <span>$pro_val</span>;
        }

        <span>//使用__get()方法获取所有属性的值</span><span>public</span><span><span>function</span><span>__get</span><span>(<span>$pro_name</span>)</span>
        {</span><span>if</span>(<span>isset</span>(<span>$pro_name</span>)){
                <span>return</span><span>$this</span>->pro_name;
            }<span>else</span>{
                <span>return</span><span>null</span>;
            }
        }
    }

    <span>$a1</span> = <span>new</span> A();
    <span>$a1</span>->n1 = <span>"aaa"</span>;
    <span>echo</span><span>$a1</span>->n1;</code>
Copier après la connexion

36 PHP面向<strong>对象</strong>


1. 抽象
36 PHP面向<strong>对象</strong>

2. 封装
36 PHP面向<strong>对象</strong>
36 PHP面向<strong>对象</strong>
36 PHP面向<strong>对象</strong>
36 PHP面向<strong>对象</strong>


3.多态
36 PHP面向<strong>对象</strong>
PHP不支持函数多态。

<code><span><span>class</span><span>A</span>
    {</span><span>public</span><span><span>function</span><span>test1</span><span>(<span>$a</span>,<span>$b</span>)</span>
        {</span><span>echo</span><span>'接受一个参数'</span>;
        }

        <span>public</span><span><span>function</span><span>test2</span><span>(<span>$a</span>)</span>
        {</span><span>echo</span><span>'接受两个参数'</span>;
        }

        <span>// 这里提供一个__call;</span><span>// 当一个<strong>对象</strong>调用某个方法,而该方法不存在,则系统自动调用__call</span><span><span>function</span><span>__call</span><span>(<span>$method</span>,<span>$p</span>)</span>
        {</span>
            var_dump(<span>$p</span>);
            <span>if</span>(<span>$method</span> == <span>"test"</span>){
                <span>if</span>(count(<span>$p1</span>)==<span>1</span>){
                    <span>$this</span>->test1(<span>$p</span>);
                }<span>else</span><span>if</span>(count(<span>$p</span>)==<span>2</span>){
                    <span>$this</span>->test2(<span>$p</span>)
                }
            }
        }
    }



    <span>$a</span> = <span>new</span> A();
    <span>echo</span><span>$a</span>->test(<span>1</span>);
也可以判断类型。</code>
Copier après la connexion

重写(覆盖):名称与参数个数必须一样
36 PHP面向<strong>对象</strong>
36 PHP面向<strong>对象</strong>


5. 抽象类
36 PHP面向<strong>对象</strong>

以上就介绍了36 PHP面向对象,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal