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, so that I can learn from the common code and functions, second, you can read other people's code to learn about some common functions and special usage. These codes are all for PHP5 encoding, so that you can fully learn the latest summary of the special usage of Make code sweet. PHP (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
StdClass
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.
ArrayAccess
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 )}
Iterator
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.
Countable
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: http://weibo.com/jameren,welcome to your attention ,.
By the way, make an advertisement:
Reference
I recently used a 15 GB ultra-high-space free web USB flash drive-@ cool disk, which can be used on PCs and mobile phones. it has no file type and size restrictions. it delivers fast transmission speeds and never loses files, 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://t.cn/aNLZje
1 floor liuzhiqiangruc
Yes. some features of PHP5 make PHP more elegant and more object-oriented.