PHP Object-oriented--final keyword class automatic loading

Source: Internet
Author: User

1.Final Keywords

Meaning: The final and final

function :

1. If a method in the parent class is declared final, the child class cannot overwrite the method.

if a class is declared final, it cannot be inherited.

2. Attributes cannot be defined as final, only classes and methods can be defined as final.

usage : Add the final keyword directly before the class or method .

---------------------------------------------------------------------------

2. automatic loading of classes

Meaning:

1. Resolve the code duplication, write the duplicate code in a folder, and let other php file calls to load automatically.

2. To solve the PHP file too many include, such as the introduction of PHP too large burden.

Implementation functions:

Spl_autoload_register () function: You can register any number of autoloader.

__autoload () : Attempting to load an undefined class classes and interfaces can also be loaded automatically (not recommended, may be deprecated). (Note: When processing a large number of programs, the program becomes bloated, cannot be implemented, the code becomes complex, which can have a significant negative impact on future system maintenance and system efficiency)

Note : Auto-load is not available for PHP's CLI interaction mode .

PHP Load File mode:
1, include,include_once,requice,requice_one Regular loading
2, __autoload ()
3, Spl_autoload_register ()

__autoload () Auto Load

<?php  function __autoload( $class ){     $file = $class . ‘.php‘ ;//拼接路径     if ( is_file ( $file )) { //判断$file是否存在      require_once ( $file ); //从$file路径导入一次    $a = new A(); Note: Because __autoload () is a function, it can only exist once. ----------------------------------------------------------------------------- spl_autoload_register () Auto LoadSpl_autoload_register () It actually creates a queue of autoload functions, executed one by one, in the order they were defined. In contrast, __autoload () can be defined only once. <?php  function loader( $class ){     $file = $class . ‘.php‘    if ( is_file ( $file )) {       require_once ( $file );     spl_autoload_register( ‘loader‘ );  $a = new A();Use anonymous functions directly: <?php    spl_autoload_register( function ( $file ){    $file = $class . ‘.php‘    if ( is_file ( $file )) {       require_once ( $file );     } });  $a = new A();

This can also work properly, when PHP is looking for classes without calling __autoload instead of calling our own defined function loader. In the same way, the following is also possible:

12345678910111213 <?php  class Loader {    public static function loadClass($class){      $file = $class . ‘.php‘    if (is_file($file)) {        require_once($file);        spl_autoload_register(array(‘Loader‘, ‘loadClass‘)); //spl_autoload_register(array(__CLASS__, ‘loadClass‘)); //spl_autoload_register(array($this, ‘loadClass‘)); $a = new A();

PHP Object-oriented--final keyword class automatic loading

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.