Table of Contents
Common magic methods
Attribute related
Method related
Comprehensive example
Home Backend Development PHP Tutorial PHP object-oriented magic method

PHP object-oriented magic method

Jun 06, 2018 am 09:57 AM
php object-oriented magic method

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

1

2

3

4

5

6

7

8

9

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

 

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

 

__isset($content)   

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

 

__unset($content)    

对不可访问的属性使用unset()时触发

Copy after login

1

2

3

4

5

6

7

8

__call(string $function_name, array $arguments)   

调用不可访问的普通方法

$function_name  被调用的方法名

$arguments      被调用方法的多个参数

 

 

__callStatic(string $function_name, array $arguments)

调用不可访问的静态方法

Copy after login

Comprehensive example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

<?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!

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)

Decrypting Python metaprogramming: from basics to advanced paradigms Decrypting Python metaprogramming: from basics to advanced paradigms Feb 19, 2024 pm 03:30 PM

Python metaprogramming basics Python metaprogramming is the ability to dynamically manipulate Python code, which makes Python a very powerful language. Metaprogramming can be implemented in the following ways: Class decorator: A class decorator is a decorator that modifies the definition of a class. It can be used to add or modify the properties and methods of a class, and can also be used to control the instantiation process of a class. defadd_method_to_class(cls):defnew_method(self):print("Thisisanewmethod")setattr(cls,"new_method",new_method)returncls@a

An in-depth explanation of PHP's object-oriented encapsulation An in-depth explanation of PHP's object-oriented encapsulation Aug 11, 2023 am 11:00 AM

In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to follow the execution order of PHP magic methods? How to follow the execution order of PHP magic methods? Apr 17, 2024 pm 09:33 PM

The execution order of PHP magic methods follows the following rules: magic methods with high priority are executed first. If both the subclass and the parent class define magic methods with the same name, the magic method of the subclass will be executed first. If a class defines both a regular method and a magic method with the same name, the regular method will be executed first.

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

What is a magic method? How to use it in Laravel What is a magic method? How to use it in Laravel Sep 26, 2022 pm 08:21 PM

What is a magic method? How to use it in Laravel? The following article will introduce to you how to apply PHP magic methods in Laravel. I hope it will be helpful to you!

PHP development: Use reflection and magic methods to achieve automatic code generation and dynamic calling PHP development: Use reflection and magic methods to achieve automatic code generation and dynamic calling Jun 15, 2023 pm 04:16 PM

In PHP development, reflection and magic methods are two commonly used techniques. When we need to automatically generate code or dynamically call certain functions, reflection and magic methods can make our code more flexible and efficient. In this article, we will explore how to use reflection and magic methods to achieve automatic code generation and dynamic invocation. Reflection is a powerful tool provided by PHP, which can help us obtain information such as classes, methods, and properties when the program is running. Through reflection, we can dynamically obtain information such as methods, properties, and annotations of a class or object, so that

Magic methods for PHP functions Magic methods for PHP functions May 19, 2023 am 08:06 AM

PHP is a server-side scripting language developed based on C language, which is widely used in web development. Functions are one of the most basic and commonly used components in programs. PHP also provides many magic methods related to functions, which can help developers better take advantage of functions. In this article, we will introduce the magic methods of PHP functions and their usage. __construct()__construct() is one of the most commonly used magic methods in PHP. It is automatically called when creating an object for initialization.

Magic Methods: Understanding __construct, __destruct and other core methods in PHP Magic Methods: Understanding __construct, __destruct and other core methods in PHP Jun 19, 2023 pm 11:22 PM

Magic methods: Understand core methods such as __construct and __destruct in PHP. In the PHP language, there are some special methods called "magic methods", including __construct, __destruct, etc. These methods play an important role in object-oriented programming in PHP. This article will explain the role and practical application of these methods. __construct method__construct method is a very important method, it is in PHP

See all articles