Home  >  Article  >  Backend Development  >  Introduction to PHP attribute overloading and method overloading (with code)

Introduction to PHP attribute overloading and method overloading (with code)

PHPz
PHPzforward
2016-07-13 10:29:281314browse

This article brings you an introduction to PHP attribute overloading and method overloading (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.


Overloaded: When accessing a non-existent or private property (insufficient permissions) or method , a series of magic methods that can be triggered

Attribute overloading

  • __set: automatically triggered when setting attributes

  • __get: Automatically triggered when obtaining attributes

  • __isset: Automatically triggered when isset and empty

  • __unset: Automatically triggered when unset

<?php/**
 * Created by IntelliJ IDEA.
 * User: 何晓宏
 * Date: 2018/10/15
 * Time: 23:46
*/class Person{
    public $name;
    private $age;
    function __construct($name,$age){
        $this->name=$name;
        $this->age=$age;
    }
    function __destruct(){
        //脚本执行结束析构自动执行
        //可以使用unset($person)
    }

    /**魔术方法__get
     * 访问属性的名字
     * @param $name
     * @return bool
     * array设置控制用户访问
     */
    public function __get($name)
    {
        $allow=array(&#39;name&#39;,&#39;age&#39;);
        if(in_array($name,$allow)){
            return $this->$name;
        }else{
            return false;
        }

    }

    /** 魔术方法__set
     * 增加属性
     * @param $name :属性名
     * @param $value :属性值
     */
    public function __set($name, $value)
    {
        $arrow=array(&#39;money&#39;);
        //判断用户的操作是否合法
        if(in_array($name,$arrow)){
            $this->$name=$value;
        }

    }

    /**魔术方法__isset
     * @param $name
     * @return bool
     */
    public function __isset($name)
    {
        //可以设置array设置权限
        return isset($this->$name);
    }

    /**魔术方法__unset
     * 删除公有私有属性 
     * @param $name
     */
    public function __unset($name)
    {
        //可以设置array设置权限
        unset($this->$name);

    }}$person=new Person(&#39;hxh&#39;,18);//unset($person);//没有魔术方法__get 无法输出echo $person->age;var_dump(isset($person->age));//增加属性$person->money=&#39;100w&#39;;echo $person->money;

Method overloading

  • __call(): Access a non-static method

  • __callStatic( ): Access a static method

<?php/**
 * Created by IntelliJ IDEA.
 * User: 何晓宏
 * Date: 2018/10/15
 * Time: 23:46
*/class Person{

    private function nojingtai(){
        //非静态方法,实例化后才会分配内存,属于实例对象
        echo __METHOD__;

    }

    protected static function jingtai(){
        //静态方法,类装载的时候就被装载到内存,属于类本身,使用效率比非静态方法要高
        echo __METHOD__;
    }

    function __construct($name,$age){
        $this->name=$name;
        $this->age=$age;
    }
    function __destruct(){
        //脚本执行结束析构自动执行
        //可以使用unset($person)
    }

    /**魔术方法__call
     * 访问一个非静态方法
     * @param $name
     * @param $arguments
     */
    public function __call($name, $arguments)
    {
        $this->$name();
    }

    /**魔术方法__callStatic
     * 访问一个静态方法
     * @param $name
     * @param $arguments
     */
    public static function __callStatic($name, $arguments)
    {
        self::$name();

    }}$person=new Person(&#39;hxh&#39;,18);//没有魔术方法之前出错$person->nojingtai();Person::jingtai();

The meaning of overloading

Control the allowed operation range of an object or class

Fault Tolerance Processing

The above is the entire content of this article. For more exciting information, please pay attention to the PHP Chinese website! ! !

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete