search
Homephp教程php手册PHP的魔术方法

1、__get/__set:对对象属性的接管
2、__call/__callStatic:控制php对象方法的使用
3、__toString:将php对象转换成字符串
4、__invoke:将一个php对象当成函数来执行时来回调这个方法

<code>class Object
{
 protected $array = array();

    function __set($key,$value)
    {
        var_dump(__METHOD__);
        $this->array[$key] = $value;
    }
    function __get($key)
    {
        var_dump(__METHOD__);
        return $this->array[$key];
    }

    function __call($func,$param)
    {
        var_dump($func,$param);
        return magic mathod __call;
    }

    static function __callStatic($func,$param)
    {
        var_dump($func,$param);
        return magic mathod __callStatic;
    }

    function __toString()
    {
        return __toString;
    }

    function __invoke($param)
    {
        var_dump($param);
        return invoke;
    }
}
</code>

$Obj = new Object();

$Obj->title = “你好”;//当对一个对象不存在的属性赋值的时候,它就会自动调用__set方法

echo $Obj->title;//对去读取一个对象不存在的属性的时候,它就会自动调用__get方法

echo $Obj->test(“hello”,”123”); //当调用一个对象不存在的方法时,就会自动调用__call方法

echo $Obj::test1(“hello1”,”1234”); //当调用一个对象不存在的静态方法时,就会自动调用__callStatic方法

echo $Obj;//当直接输出一个对象时(因为对象不能直接输出),就会自动调用__toString方法把对象转换成字符串

echo $Obj(“hello”);//当将一个对象当成一个函数来使用的时候,就会自动调用__invoke方法



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function