Makecodesweet. PHP summary of non-general usage (1)

Source: Internet
Author: User
Tags rewind
Makecodesweet. summary of some special PHP Usage (1) & nbsp; a recent project is coming to an end, and the work is coming to an easy start, in my spare time, I will take a look at some PHP frameworks and code in some excellent open-source software. I can refer to the common code and functions, and I will read the Make code sweet. summary of some special PHP Usage (1)
A recent project is coming to an end and the work is coming to an easy time. in my spare time, I will take a look at some PHP frameworks and the code in some excellent open-source software, first, you can learn from the common code and functions. second, you can read other people's code to learn about some less common functions and special usage. These codes are all PHP 5 encoded so that you can fully learn the latest features. The following is a summary of the special usage of the code during this period.
First of all, let's talk about the syntactic sugar in PHP. in complicated PHP applications, it tends to use MVC frameworks or generic frameworks, or create new ones by yourself, for the sake of beauty and ease of use, a little syntactic sugar is indispensable. The following content is useful.
1. magic methods to enrich objects
PHP5 adds several magic methods, some of which allow objects to execute non-existent methods and obtain non-existent attributes. This feature should be the most well-known advanced usage. if you do not know it, you can view Overloading in PHP Manual.

_ Call and _ callStatic
Triggered when the method of the execution object does not exist. the prototype is as follows:
mixed __call ( string $name , array $arguments )mixed __callStatic ( string $name , array $arguments )

_ Set and _ get ,__ isset and _ unset
Triggered when an object attribute does not exist in the operation. the prototype is as follows:
void __set ( string $name , mixed $value )mixed __get ( string $name )bool __isset ( string $name )void __unset ( string $name )

2. make the array object-oriented

Sometimes we want to access elements through the object method, we can use stdClass, which is similar to castrated using the object method-> accessing the array of elements, the monks feel that using it is simply a pursuit of visual feeling and less than two characters. but then again, these are also very important in programming aesthetics, aren't they?
From Array to stdClass, you can traverse the Array and add it by yourself. You can use the get_object_vars function to convert stdClass to Array.
The most common examples are mysql_fetch_object and json_decode.

3. make the array 'Omnipresent'
Someone once said that PHP is oriented to array programming. it is reasonable to think carefully. Array Operations in PHP are very powerful and convenient. array operations mainly include [] operators, foreach, and count functions, if you want your class to have these functions, you can, of course, implement the following interface.

This interface allows the object to support [] operations. the prototype is as follows:
ArrayAccess {/* Methods */abstract public boolean offsetExists ( string $offset )abstract public mixed offsetGet ( string $offset )abstract public void offsetSet ( string $offset , string $value )abstract public void offsetUnset ( string $offset )}

This interface allows the object to support foreach. the prototype is as follows:
Iterator extends Traversable {/* Methods */abstract public mixed current ( void )abstract public scalar key ( void )abstract public void next ( void )abstract public void rewind ( void )abstract public boolean valid ( void )}

This interface is relatively complex because it refers to the execution sequence of interface functions in the foreach operation. To put it simply, rewind/next-> valid-> current-> key. for details, refer to the relevant section in PHP Manual, which is clearly written.

This interface allows the object to support the count function. the prototype is as follows:
Countable {/* Methods */abstract public int count ( void )}

I will summarize it here today. in the future, I will continue to write summary of session processing, autoload, error processing, and other related content to strengthen my memory and facilitate future queries.
My Weibo address:,welcome to your attention ,.
By the way, make an advertisement:
I recently used a 15 GB ultra-high-space free network USB flash drive-@ cool disk, which can be used on PCs and mobile phones. it has no file type and size restrictions, fast transmission speed, and files will never be lost, let's try it together! Register through the invitation link below, and you and I will get an additional MB of cool disk space! Http://
Liuzhiqiangruc on the first floor is good. some features of PHP5 make PHP more elegant and more object-oriented.

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: 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.