从PHP 5以后的版本,PHP中的类就可以使用魔术方法了。其规定以两个下划线(__)开头的方法都保留为魔术方法,所以建议大家函数名最好不用__开头,除非是为了重载已有的魔术方法。 1、__get、__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的。 ◆__get( $property ) 当调用一个未定义的属性时,此方法会被触发,传递的参数是被访问的属性名。 ◆__set( $property, $value ) 给一个未定义的属性赋值时,此方法会被触发,传递的参数是被设置的属性名和值。 这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性)。 2、__isset、__unset ◆__isset( $property ) 当在一个未定义的属性上调用isset()函数时调用此方法。 ◆__unset( $property ) 当在一个未定义的属性上调用unset()函数时调用此方法。 与__get方法和__set方法相同,这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性)。 3、__call __call( $method, $arg_array ) 当调用一个未定义的方法是调用此方法。 这里的未定义的方法包括没有权限访问的方法;如果方法不存在就去父类中找这个方法,如果父类中也不存在就去调用本类的__call()方法,如果本类中不存在__call()方法就去找父类中的__call()方法。 4、__autoload __autoload 函数,它会在试图使用尚未被定义的类时自动调用。通过调用此函数,脚本引擎在 PHP 出错失败前有了最后一个机会加载所需的类。 如果要定义一个全局的自动加载类,则必须用spl_autoload_register()方法将处理类注册到PHP标准库: 注意: 在 __autoload 函数中抛出的异常不能被 catch 语句块捕获并导致致命错误,所以应该在函数本身做捕获。 5、__construct、__destruct ◆__construct 构造方法,当一个对象创建时调用此方法,相对于PHP4使用此方法的好处是:可以使构造方法有一个独一无二的名称,无论它所在的类的名称是什么.这样你在改变类的名称时,就不需要改变构造方法的名称。 ◆__destruct 析构方法,PHP将在对象被销毁前(即从内存中清除前)调用这个方法。默认情况下,PHP仅仅释放对象属性所占用的内存并销毁对象相关的资源,析构函数允许你在使用一个对象之后执行任意代码来清除内存。当PHP决定你的脚本不再与对象相关时,析构函数将被调用。 在一个函数的命名空间内,这会发生在函数return的时候。对于全局变量,这发生于脚本结束的时候。如果你想明确地销毁一个对象,你可以给指向该对象的变量分配任何其它值.通常将变量赋值勤为NULL或者调用unset。 6、__clone PHP 5中的对象赋值是使用的引用赋值,如果想复制一个对象则需要使用clone方法,在调用此方法是对象会自动调用__clone魔术方法,如果在对象复制需要执行某些初始化操作,可以在__clone方法实现。 7、__toString __toString方法在将一个对象转化成字符串时自动调用,比如使用echo打印对象时。 如果类没有实现此方法,则无法通过echo打印对象,否则会显示:Catchable fatal error: Object of class test could not be converted to string in,此方法必须返回一个字符串。 在PHP 5.2.0之前,__toString方法只有结合使用echo() 或 print()时 才能生效。PHP 5.2.0之后,则可以在任何字符串环境生效(例如通过printf(),使用%s修饰符),但 不能用于非字符串环境(如使用%d修饰符)。从PHP 5.2.0,如果将一个未定义__toString方法的对象 转换为字符串,会报出一个E_RECOVERABLE_ERROR错误。 8、__sleep、__wakeup ◆__sleep 串行化的时候用 ◆__wakeup 反串行化的时候调用 serialize() 检查类中是否有魔术名称__sleep 的函数。如果这样,该函数将在任何序列化之前运行。它可以清除对象并应该返回一个包含有该对象中应被序列化的所有变量名的数组。 使用__sleep 的目的是关闭对象可能具有的任何数据库连接,提交等待中的数据或进行类似的清除任务。此外,如果有非常大的对象而并不需要完全储存下来时此函数也很有用。 相反地,unserialize() 检查具有魔术名称 __wakeup 的函数的存在。如果存在,此函数可以重建对象可能具有的任何资源。使用 __wakeup 的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务。 9、__set_state 当调用var_export()时,这个静态 方法会被调用(自PHP 5.1.0起有效)。 本方法的唯一参数是一个数组,其中包含按array(’property’ => value, …)格式排列的类属性。 10、__invoke(PHP 5.3.0以上版本有效) 当尝试以调用函数的方式调用一个对象时,__invoke 方法会被自动调用。 11、__callStatic(PHP 5.3.0以上版本有效) 它的工作方式类似于__call() 魔术方法,__callStatic() 是为了处理静态方法调用。 PHP 确实加强了对 __callStatic() 方法的定义;它必须是公共的,并且必须被声明为静态的。同样,__call() 魔术方法必须被定义为公共的,所有其他魔术方法都必须如此。<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li><span>class Loader </span></li>
<li class="alt"><span>{ </span></li>
<li><span>static function autoload_class($class_name) </span></li>
<li class="alt"><span>{ </span></li>
<li><span>//寻找正确的$class_name类,并引入,没有则抛出异常 </span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span> </span></li>
<li><span>/** </span></li>
<li class="alt"><span>* 设置对象的自动载入 </span></li>
<li><span>* spl_autoload_register — Register given function as __autoload() implementation </span></li>
<li class="alt"><span>*/ </span></li>
<li><span>spl_autoload_register(array(‘Loader’, ‘autoload_class’)); </span></li>
<li class="alt">
<span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">new</span><span> Test();//Test没用require就实例化,实现自动加载,很多框架就用这种方法自动加载类 </span>
</li>
</ol>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

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

WebStorm Mac version
Useful JavaScript development tools