How to use php magic methods __get() and __set()

青灯夜游
Release: 2023-03-11 09:54:01
Original
1953 people have browsed it

"__set()" is automatically called when assigning a value to an undefined attribute, the syntax is "public function __set(key,value){}"; "__get()" is called when an undefined attribute is called, Syntax "public function __get(name){}".

How to use php magic methods __get() and __set()

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The magic method in PHP usually starts with __( It starts with two underscores) and does not need to be explicitly called but is automatically called under certain conditions.

Magic method Function
__set() is giving undefined Automatically called when assigning a property
__get() Automatically called when calling an undefined property

1. __set() method

When assigning a value to a class attribute that is undefined or invisible in the current environment, the __set() method will be automatically called. The syntax format for defining this method is as follows:

public function __set($key, $value){
    ... ... ;
}
Copy after login

Among them, the parameter $key is the name of the variable to be operated, and $value is the value of the variable $key.

[Example] The following uses a simple example to demonstrate the use of the __set() method.

<?php
    class Website{
        public $name;
        private $url;
        public function __set($key, $value){
            echo &#39;为“&#39;.$key.&#39;”赋值“&#39;.$value.&#39;”失败!<br>&#39;;
        }
    }
    $object          = new Website();
    $object -> name  = &#39;php中文网&#39;;
    $object -> url   = &#39;https://www.php.cn/&#39;;
    $object -> title = &#39;PHP教程&#39;;
?>
Copy after login

The running results are as follows:

为“url”赋值“https://www.php.cn/”失败!
为“title”赋值“PHP教程”失败!
Copy after login

Using the characteristics of the __set() method, we can use the __set() method to assign or modify the attributes modified with the private keyword in the class. . As shown below:

<?php
    class Website{
        public $name;
        private $url = &#39;&#39;;
        public function __set($key, $value){
            if(isset($this->$key)){
                $this -> $key = $value;
            }else{
                echo &#39;为“&#39;.$key.&#39;”赋值“&#39;.$value.&#39;”失败!<br>&#39;;
            }
        }
        public function getUrl(){
            echo $this -> url;
        }
    }
    $object          = new Website();
    $object -> name  = &#39;php中文网&#39;;
    $object -> url   = &#39;https://www.php.cn/&#39;;
    $object -> title = &#39;PHP教程&#39;;
    $object -> getUrl();
?>
Copy after login

The running results are as follows:

为“title”赋值“PHP教程”失败!
https://www.php.cn/
Copy after login

2. The __get() method

is not defined or is not defined when calling or obtaining the current environment. When the class attribute is invisible, the __get() method will be automatically called. The syntax format for defining this method is as follows:

public function __get($name){
    ... ... ;
}
Copy after login

The parameter $name is the name of the variable to be operated.

[Example] The following uses a simple example to demonstrate the use of the __get() method.

<?php
    class Website{
        public $url = &#39;https://www.php.cn/&#39;;
        private $name = &#39;php中文网&#39;;
        public function __get($name){
            echo &#39;获取:“&#39;.$name.&#39;”失败!&#39;;
        }
    }
    $object = new Website();
    echo $object -> url.&#39;<br>&#39;;
    echo $object -> name.&#39;<br>&#39;;
    echo $object -> title.&#39;<br>&#39;;
?>
Copy after login

The running results are as follows:

https://www.php.cn/
获取:“name”失败!
获取:“title”失败!
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php magic methods __get() and __set(). 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