Make code sweet.PHP特殊用法的一些总结(1)
最近一个项目接近尾声了,工作慢慢轻松了下来,闲暇时就看看一些PHP的框架和一些优秀的开源软件中的代码,一来可以借鉴一下其中的常用代码和函数,二来通过阅读别人的代码了解一些平时不太常见的函数以及特殊用法。这些代码都是针对PHP 5编码的,这样才能充分学习最新的特性。下面是我这段时间阅读代码的特殊用法的一些总结。
首先我们来说说PHP中的语法糖,在复杂的PHP应用中,一般都趋向于使用MVC框架,或者通用的,或者自己造新的,为了美观易用都少不了撒上一点语法糖。以下内容会有用。
1.魔法方法,让对象更丰富一点
PHP5新增了几个魔法方法,其中一些方法可以让对象执行其不存在的方法和获取不存在的属性。这个特性应该是最家喻户晓的高级用法,如果你不知道,可以查看PHP Manual里Overloading这一节。
__call and __callStatic
在执行对象的不存在的方法时触发,原型如下:
mixed __call ( string $name , array $arguments ) mixed __callStatic ( string $name , array $arguments )
__set and __get ,__isset and __unset
在操作不存在的对象属性时触发,原型如下:
void __set ( string $name , mixed $value ) mixed __get ( string $name ) bool __isset ( string $name ) void __unset ( string $name )
2.让数组面向对象一点
stdClass
有时候我们希望通过对象方式访问元素,那可以用stdClass,它类似于阉割的使用对象方式->访问元素的array,鄙人感觉使用它完全是追求视觉上的感觉和少打两个字符,不过话说回来,这些在编程美学上也是很重要的不是吗?
从Array转stdClass,可以遍历数组自己添加。从stdClass转Array,可以使用函数get_object_vars来获取。
最常见使用它的例子就是mysql_fetch_object和json_decode。
3.让数组‘无处不在’
曾经看帖有人说PHP是面向数组编程,细细想来不无道理,PHP中的数组操作十分强大方便,数组操作主要包括[]操作符,foreach,和count函数,想让你的类也拥有这些功能,当然可以,实现如下接口吧。
ArrayAccess
实现该接口可以让对象支持[]操作,原型如下:
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
实现该接口可以让对象支持foreach,原型如下:
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 ) }
这个接口相对比较复杂,在于foreach操作中接口函数的执行顺序。简单来说就是:rewind/next->valid->current->key,具体可以参看PHP Manual中相关部分,这部分写的很清楚的。
Countable
实现该接口可以让对象支持count函数,原型如下:
Countable { /* Methods */ abstract public int count ( void ) }
今天就先总结到这里,日后我准备继续写session处理,autoload,错误处理等相关内容的一些总结纲要,以强化自己的记忆,方便日后查询。
我的微博地址:http://weibo.com/jameren,欢迎大家关注,

顺便做个广告:
我最近使用了15G超大空间免费网络U盘―@酷盘,可在PC及手机上使用,无文件类型及大小限制,传输速度快,文件永不丢失,一起来体验吧!通过下面的邀请链接注册,你和我都将额外获得256MB的酷盘空间! http://t.cn/aNLZje

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft