PHP Object-oriented-Magic method

Source: Internet
Author: User

__construct () constructor

__destruct () destructor

__sleep () serialize serialized when called

__wakeup () Unserialize called when deserializing

__clone () Detecting cloned objects

Magic method for Property overloading:

Set,get,sset,unset

__set () When assigning a value to a property that is not accessible, it is automatically called to get two parameters, the property name of the current operation, and the property value!

Typical, __set function, used to strictly control the object structure, and batch processing can be modified properties!

__get ()

When accessing inaccessible properties, it is automatically called

The required parameters are: One, the property name for the current operation

__unset ()

It can be called automatically when you delete an inaccessible property!

Requires a parameter for the property name of the current operation.

At this point, you can take advantage of the business logic to complete the deletion of property processing!

__isset ()

is automatically called when an inaccessible property is determined to exist

Requires a parameter, property name

Note that this function needs to return true, or false, to indicate whether the property exists:

Method overloading The Magic method:

__call ()

When accessing a non-accessible object method, the current Magic method is triggered!

Required Parameters:

2 parameters, the first is the current method name, and the second is the list of arguments to use when calling!

A typical role,

First, give a friendly hint

Second, perform the default action!

static __callstatic ()

Similar to __call, when statically invoking an inaccessible method, it is executed automatically!

Magic Method

In a particular case, the method that will be called automatically is usually responsible for completing a block of independent functions called the Magic Method!

Characteristics:

1, requires user script definition, does not define do not execute!

2, the naming method is to start with __!

Summarize:

__construct

__destruct

__sleep

__wakeup

__set

__get

__isset

__unset

__clone

__call

__callstatic

__autoload ()//Magic function

__invoke ()

An object, as a function call, fires the object's __invoke () method, which can be called by this method, and cannot be called without the method!

Recall anonymous function

Why, anonymous function object (Closure class object) Yes, but ordinary class object does not?

PHP built-in closure class has the __invoke () method: __invoke is an integral part of PHP's implementation of anonymous functions!

__tostring ()

Convert to String meaning!

When you use an object as a string, the object's Magic method is called automatically!

PHP Object-oriented-Magic method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.