Home > Backend Development > PHP Tutorial > Beginner's Guide to PHP: Magic Methods

Beginner's Guide to PHP: Magic Methods

WBOY
Release: 2023-05-20 10:28:01
Original
1307 people have browsed it

As a widely used development language, PHP has rich features, and its magic method is one of them. This article will introduce you to the magic method in PHP.

1. What is a magic method?

In PHP, magic method refers to a type of method that can be automatically called. These methods use special names, starting and ending with a double underscore (__), and have special behavior when they are called.

2. Commonly used magic methods in PHP

  1. __construct()

When creating a new object, the __construct() function will be called . Usually used to initialize the properties of an object or perform other necessary operations.

Example:

class MyClass {
    private $str;

    public function __construct($str) {
        $this->str = $str;
    }

    public function printStr() {
        echo $this->str;
    }
}

$obj = new MyClass('hello');
$obj->printStr(); //输出 hello
Copy after login
  1. __destruct()

When the object instance is destroyed, the __destruct() function will be called. Usually used to clean up some resources or perform other necessary operations.

Example:

class MyClass {
    public function __destruct() {
        echo "Object destroyed.";
    }
}

$obj = new MyClass();
unset($obj); //销毁对象
Copy after login
  1. __toString()

When an object needs to be represented as a string, the __toString() function will be automatically called. It should be noted that this method must return a string.

Example:

class MyClass {
    public function __toString() {
        return "This is MyClass";
    }
}

$obj = new MyClass();
echo $obj; //输出 This is MyClass
Copy after login
  1. __get() & __set()

__get() method is blocked when accessing an inaccessible or non-existent property Automatically called; the __set() method is automatically called when assigning a value to a property that does not exist. These two methods can be used to control access permissions.

Example:

class MyClass {
    private $name;

    public function __get($prop) {
        if($prop == 'name') {
            return $this->name;
        } else {
            return "Property $prop not found.";
        }
    }

    public function __set($prop, $value) {
        if($prop == 'name') {
            $this->name = $value;
        } else {
            echo "Property $prop not found.";
        }
    }
}

$obj = new MyClass();
$obj->name = 'Tom';
echo $obj->name; //输出 Tom
echo $obj->age; //输出 Property age not found.
Copy after login
  1. __call() & __callStatic()

__call() method is automatically called when accessing a non-existent method; The __callStatic() method is automatically called when accessing a non-existent static method. These two methods can be used to handle method calls dynamically.

Example:

class MyClass {
    public function __call($method, $args) {
        echo "Method $method not found.";
    }

    public static function __callStatic($method, $args) {
        echo "Static method $method not found.";
    }
}

$obj = new MyClass();
$obj->test(); //输出 Method test not found.
MyClass::demo(); //输出 Static method demo not found.
Copy after login

3. Summary

The above are commonly used PHP magic methods. Of course, there are other magic methods such as __isset(), __unset(), __sleep (), __wakeup(), __clone(), etc. The power of magic methods is that they can simplify code and improve development efficiency. However, excessive use of magic methods can make the code difficult to understand and debug, so you need to pay attention to moderation when using magic methods.

The above is the detailed content of Beginner's Guide to PHP: Magic Methods. 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