PHP 3: PHP class automatic loading

Source: Internet
Author: User
Tags autoload
PHP comprehension 3: PHP classes automatically load [convert] to: www.liuhui.infoarticle78.html? The spl_autoload_register () function should be one of the most used and very core functions in mainstream frameworks. it can automatically register functions and classes and implement functions similar to the _ autoload () function, this simplifies the call and loading of classes and improves the efficiency of work. here we will talk about this PHP comprehension through an experiment: automatically loading PHP classes]

Turn: http://www.liuhui.info/article78.html

?

The spl_autoload_register () function should be one of the most used and very core functions in mainstream frameworks. it can automatically register functions and classes and implement functions similar to the _ autoload () function, it simplifies class calling and loading and improves work efficiency. here we will talk about some features of this function through an experiment.

Function prototype
BoolSpl_autoload_register? ([? Callback $ autoload_function? [,? Bool $ throw = true? [,? Bool $ prepend = false? ])

Version compatibility
PHP 5> = 5.1.2

?

Step 1: Use the spl_autoload_register () function to register the load () method.

 

? The lib. php file code is as follows:

 

? Note: The lib. php file is a className class and an onlyMethod function.

? Note: class definitions must be included in the PHP file loaded automatically.

?

Step 2: Call the automatic loading class

$class = new className();$class->method();onlyMethod();

? Output: a method in class
???????????? Method only

?

Step 3: directly call the function

onlyMethod();

Note: The onlyMethod () function in the lib. php file is called directly without an instantiation class.

Output:
Fatal error: Call to undefined function onlyMethod () in '... (omitted path )'

?

Step 4: instantiate the className class and then directly call

$class = new className();onlyMethod();

Output: method only

The above four steps show that if the loaded file contains a function, you must instantiate the class. Otherwise, an exception occurs. Call to undefined function is incorrect, pay attention to the specific usage.

?

?

?

?

?

?

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.