Home headlines php magic method

php magic method

Jun 28, 2018 pm 02:26 PM

* 1. Magic methods: __get(), __set() to implement attribute queryers and setters

* 2. Magic methods have been introduced before and need to be triggered by specific scenarios and automatically called by the object

* 3.__get($name): Automatically triggered when the object's private attributes or non-existent attributes are obtained externally through the object

* 4.__set($name,$value): Externally set private Automatically triggered when an attribute or attribute value does not exist

* 5. The magic method can be applied to all existing or non-existing class attributes, and there is no need to create a corresponding access interface for each attribute

class GirlFriend3 {
    //声明属性
    private $name;
    private $age;
    private $stature;
    private $data=[];
    
    //构造方法
    public function __construct($name='',$age=0,array $stature=[]) 
    {
        $this->name = $name;
        $this->age = $age;
        $this->stature = $stature;
    }
    
    //魔术方法:查询器
    public function __get($name)
    {
//        return $this->$name;
        //加入检测:访问不存在的属性时给出提示信息
//        return isset($this->$name)?$this->$name:'无此属性';
        
        //如果类中添加一个自定义的数据收集器$data,就从这里取值
        $msg = null;
        if (isset($this->$name)) {
            $msg = $this->$name;
        } elseif (isset($this->data[$name])) {
            $msg = $this->data[$name];
        
        } else {
            $msg = '无此属性';
        }
        
        return $msg;
    }
    
    //魔术方法:设置器
    public function __set($name, $value)
    {
        //不做检测直接设置
//        $this->$name = $value;
        
        //完善设置器,实现对不存在属性的创建
        //如果访问的是已存在的属性,则直接输出
        if (isset($this->$name)) {
            $this->$name = $value; //输出属性
        } else {
            //如果属性不存在,则创建它并保存到类属性$data数组中
            $this->data[$name] = $value;
        }
    }
}
Copy after login
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)

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.

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.

PHP Class Usage Example Analysis: Learn how to define and use classes PHP Class Usage Example Analysis: Learn how to define and use classes Mar 11, 2024 pm 02:21 PM

In PHP programming, class is a very important concept. It can help us better organize and manage code, and improve code reusability and maintainability. This article will explain how to define and use PHP classes through example analysis, and provide specific code examples to help readers better understand. 1. Class definition In PHP, use the keyword class to define a class. The sample code is as follows: classPerson{public$name;

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

Understanding PHP classes and objects: Basics of object-oriented programming Understanding PHP classes and objects: Basics of object-oriented programming May 11, 2023 am 09:12 AM

PHP is an open source server-side scripting language that can be embedded in HTML and create websites using standard PHP syntax or mixed HTML and PHP code. Object-oriented programming (OOP) is a paradigm of modern programming languages, and PHP is one of the languages ​​that supports OOP. OOP programming can help developers better organize and maintain complex applications and improve code reusability and scalability. In PHP, classes and objects are the two core concepts of object-oriented programming. This article will introduce PHP

PHP class abstraction PHP class abstraction Aug 30, 2023 pm 11:17 PM

IntroductionInobjectorientedprogramming,anabstractclassistheonethatcanbeinstantiated,i.e.itisnotpossibletodeclareobjectofsuchclass.PHPsupportsconceptofabstarctclasssinceversion5.0Aclassdefinedwithabstractkeywordbecomesanabstractclass.Further,anyclass

How to use magic methods to enhance the functionality of PHP classes How to use magic methods to enhance the functionality of PHP classes Aug 03, 2023 pm 10:43 PM

How to use magic methods to enhance the functionality of PHP classes Introduction: PHP has many powerful features and built-in functions, one of which is magic methods. Magic methods are a set of special functions that can be called implicitly in a class to enhance its functionality. This article will explore how to properly utilize magic methods to enhance the functionality of PHP classes and provide some practical code examples. 1. Construction method and destruction method Construction method (__construct) and destructor method (__destruct) are the two most basic magic

Explore the role and necessity of abstract methods in PHP classes Explore the role and necessity of abstract methods in PHP classes Mar 20, 2024 am 09:33 AM

Title: Exploring the role and necessity of abstract methods in PHP classes Abstract methods are an important concept in object-oriented programming, and they play a key role in PHP classes. This article will deeply explore the role and necessity of abstract methods in PHP classes, and demonstrate its usage and advantages through specific code examples. What is an abstract method? In PHP, abstract methods refer to methods defined in abstract classes without specific implementation. Abstract methods must be implemented in the subclass, otherwise the subclass must also be declared as an abstract class. By defining abstract methods, we can want