PHP object-oriented magic method

不言
Release: 2023-03-29 22:14:02
Original
1053 people have browsed it

This article mainly introduces the object-oriented magic methods in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Common magic methods

Attribute related

__set($property, $value)     给不可访问的属性赋值

__get($propertyName)         调用不可访问的属性

__isset($content)    
对不可访问的属性使用empty()、isset()时触发

__unset($content)     
对不可访问的属性使用unset()时触发
Copy after login

Method related

__call(string $function_name, array $arguments)    
调用不可访问的普通方法
$function_name  被调用的方法名
$arguments      被调用方法的多个参数


__callStatic(string $function_name, array $arguments)
调用不可访问的静态方法
Copy after login

Comprehensive example

<?php

class Example
{
    // 可访问的属性
    public $price = 111;

    //不可访问的属性
    private $secret = &#39;我是不可访问的属性&#39;;
    private $age = 23;

    //不可访问的普通方法
    private function donTell()
    {
        echo &#39;不能说的秘密&#39;;
    }

    //不可访问的静态方法
    private static function super()
    {
        echo &#39;我是不可访问的静态方法&#39;;
    }

    //魔术方法
    public function __set($property, $value)
    {
        echo &#39;给不可访问的属性赋值,您要设置的属性是&#39;.$property.&#39;,值是&#39;.$value;
        $this -> $property = $value;
    }

    public function __get($propertyName)
    {
        echo &#39;调用私有属性&#39;.$propertyName.&#39;,它的属性值是&#39;.$this -> $propertyName;
    }

    public function __isset($content)
    {
        echo &#39;你在对不可访问的属性进行empty()、isset()操作&#39;;
        isset($this->$content);
    }

    public function __unset($content)
    {
        echo &#39;你在对不可访问的属性进行unset()操作&#39;;
        isset($this->$content);
    }


    public function __call($function_name,$args)
    {
        echo &#39;你在调用不可访问的普通方法&#39;;
        echo &#39;方法名是&#39;.$function_name;
        var_dump($args);
    }

    public static function __callStatic($function_name,$args)
    {
        echo &#39;调用不可访问的静态方法&#39;;
        echo &#39;方法名是&#39;.$function_name;
    }

}


//实例化对象
$e1 = new Example();


/******* 触发各种魔术方法 ***********/

// 测试__set方法
$e1 -> secret = &#39;我是秘密&#39;;
echo &#39;<hr/>&#39;;

// 测试__get方法
$e1 -> age;
echo &#39;<hr/>&#39;;

// 测试__isset()方法
echo isset($e1 -> age);
echo &#39;<hr/>&#39;;

// 测试__unset方法
unset($e1 -> age);
echo &#39;<hr/>&#39;;

// 测试__call方法
$e1 -> donTell([1,2,3]);
echo &#39;<hr/>&#39;;

// 测试__callStatic方法
$e1 -> super();



?>
Copy after login

Related recommendations:

php object-oriented static Methods, properties and constants

php object-oriented constructors and destructors

The above is the detailed content of PHP object-oriented magic method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template