Home Backend Development PHP Tutorial Description of magic methods in php

Description of magic methods in php

Jun 28, 2018 pm 03:40 PM

* Magic method:

* 1. Methods in the class that start with double underscores are built-in by the system. User methods should not start with double underscores;

* 2. Magic The method is automatically triggered by the system under certain conditions and cannot be called directly by the user;

* Note: It is a good habit to add a single underscore in front of the private members in the class;

* For example: private $_salary; private function _listUsers(){...}

* Use magic methods to implement several object access interceptors

* The so-called interceptor: it is some errors or illegal access to users Perform detection and control.

* We have learned __get($fieldName) and __set() before, which are the two most commonly used interceptors

* Now we will learn another set: __isset($fieldName) and __unset($fieldName)

* 1. __isset(): It will be automatically called when checking whether a class attribute exists outside the class

* 2. __unset(): When checking whether a class attribute exists outside the class When a class attribute is destroyed externally, it will be automatically called.

class Demo 
{
    private $name = 'peter';
    private $email = 'peter@php.cn';
    
    //当在类外使用isset()检测某个属性是否存在时自动调用
    public function __isset($name)
    {
        //对访问进行过滤:如果属性名是'name',返回false,否则允许访问
        //即除了'name'属性外的其它属性允许外部进行isset()检测
        return ($name=='name') ? false : true;
    }
}
//实例化 Demo 类
$obj = new Demo;
//检测$obj中是否有name属性,返回 
echo isset($obj->name)?'存在':'不存在';
echo &#39;<hr>&#39;;
Copy after login

Detects whether the email attribute exists in $obj and returns that it exists because __isset() in the class returns true

echo isset($obj->email)?&#39;存在&#39;:&#39;不存在&#39;;
Copy after login

Use unset() externally To destroy class attributes

The above is the detailed content of Description of magic methods in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Naming conventions and best practices for PHP methods Naming conventions and best practices for PHP methods Feb 29, 2024 pm 01:51 PM

Naming Conventions and Best Practices for PHP Methods As a popular server-side scripting language, PHP is widely used to develop websites and web applications. In PHP development, methods (functions) are a very important part. Good naming conventions and best practices can improve the readability, maintainability and scalability of the code. This article will share some norms and best practices about PHP method naming, while providing specific code examples. Method naming convention 1. Use meaningful and descriptive names. The name of a method should accurately describe the method.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In-depth understanding of the concept of PHP method body In-depth understanding of the concept of PHP method body Mar 29, 2024 am 08:00 AM

In PHP programming, the method body refers to the function encapsulated in the class, which is a code block used to implement a specific function. Through the method body, we can separate the functional code and improve the maintainability and reusability of the code. In this article, we will delve into the concept of PHP method bodies and illustrate them with specific code examples. 1. Basic concepts of classes and methods First, let us understand the basic concepts of classes and methods. Class is the basic concept of object-oriented programming, which represents a template or blueprint for objects with similar characteristics and behaviors.

Detailed explanation of PHP method parameter passing and return value Detailed explanation of PHP method parameter passing and return value Feb 29, 2024 pm 06:18 PM

Detailed explanation of PHP method parameter passing and return value In PHP, method parameter passing and return value are very important concepts, and they play a vital role in program development. This article will discuss the specific usage of method parameter passing and return value in PHP in detail, and attach corresponding code examples to help readers better understand. Method Parameter Passing In PHP, methods can accept parameters and process them. There are mainly the following ways to pass method parameters: call by value (default): In PHP, by default, the method is called by value.

The definition and usage of PHP method body The definition and usage of PHP method body Mar 28, 2024 pm 03:03 PM

PHP is a server-side scripting language widely used in Web development. The definition and usage of PHP method bodies are very important parts in PHP programming. The method body refers to the specific implementation code part of the function. By defining the method body, code with the same function can be encapsulated and reused, improving the maintainability and readability of the code. In PHP, the definition of method bodies follows certain grammatical rules. The definition and use of PHP method bodies will be introduced below, with specific code examples. The definition of PHP method body is in PHP.

PHP method definition and usage guide PHP method definition and usage guide Feb 29, 2024 pm 05:51 PM

PHP method definition and usage guide PHP is a powerful server-side scripting language widely used in web development. In PHP, methods (also called functions) are a mechanism for encapsulating reusable blocks of code. This article will introduce you to the definition and use of PHP methods, with specific code examples for reference. Definition of method In PHP, the definition of method follows the following syntax format: function method name (parameter 1, parameter 2,...) {//method body}

Explain the __invoke magic method in PHP. Explain the __invoke magic method in PHP. Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Best practices for magic methods in PHP programs Best practices for magic methods in PHP programs Jun 07, 2023 am 08:01 AM

Magic methods are one of the important features in PHP programs. They are called magic methods because they provide a way to automatically perform certain operations in a class without calling them explicitly. In PHP, there are multiple magic methods, including __construct, __destruct, __get, __set, __call, __toString, __sleep, __wakeup, __isset, __unset, etc. These methods not only help improve

See all articles