Home php教程 php手册 探秘PHP中的魔术方法

探秘PHP中的魔术方法

Jun 06, 2016 pm 07:57 PM
linux php Explore method Community Enter magic

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 有了构造函数,我们可以像下面的代码一样实例化一个Device类的对象。 1 $device=new Device('iPhone',$battery); 2.析构函数:__destruct() 与构造函数相反,析构函数是在对象被销毁时由系统自

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

  有了构造函数,我们可以像下面的代码一样实例化一个Device类的对象。

  1 $device=new Device('iPhone',$battery);

  2.析构函数:__destruct()

  与构造函数相反,析构函数是在对象被销毁时由系统自动调用的。在类中定义构造函数的方式如下:

  public function __destruct(){

  $this->disconnect();

  }

  在这个析构函数中,在对象被销毁之前,析构器先调用了类中的disconnect()函数。

  三、属性重载

  根据PHP手册中的说明,PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法。我们是通过魔术方法(magic methods)来实现的。

  当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用,换句话说,当访问一个类中可访问的方法或属性时,重载方法不会被调用。所有的重载方法都必须被声明为 public.

  1.__get()

  当读取不可访问属性的值时,__get() 会被调用。

  2.__set()

  在给不可访问属性赋值时,__set() 会被调用。

  3.__isset()

  当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。

  4.__unset()

  当对不可访问属性调用 unset() 时,__unset() 会被调用。

  四、将对象转换为字符串:__toString()

  在我们试图将对象当做一个普通字符串来对待时,__toString方法会被调用。比如,当我们打印一个对象时,就会自动调用该函数,如:echo $对象名,如果没有定义__toString方法,那么PHP将返回一个错误。

  五、克隆对象:__clone()

  当我们克隆一个对象时,魔术方法__clone()会被自动调用

  六、对象序列化

  序列化是将任何数据转化为字符串格式的一个过程。通过序列化,我们可以将一个完整的对象保存在一个文件或者保存在数据库中。有关序列化的一个问题是,并不是所有的数据都可以被序列化,例如一个数据库连接。

  1.__sleep()

  当我们对一个对象使用serialize()函数时,__sleep()函数会被调用。

  2.__wakeup()

  当我们对一个对象使用unserialize()函数时,__wakeup()函数会被调用。

  七、方法重载

  1.__call()

  在对象中调用一个不可访问方法时,__call() 会被调用。

  2.__callStatic() (PHP 5.3)

  用静态方式中调用一个不可访问方法时,__callStatic() 会被调用。

  八、将对象当作函数使用

  1.__invoke

  当我们试图将对象当做函数来使用时,该方法会被调用。

  九、自动加载类

  1.__autoload()

  该方法是我们在实例化一个类时,允许程序自动寻找类文件,并将类文件加载到当前脚本,可以简化很多我们对require的使用。

  [1] [2] 

探秘PHP中的魔术方法

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

gate.io official website registration installation package link gate.io official website registration installation package link Feb 21, 2025 pm 08:15 PM

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

See all articles