PHP automatic loading
Automatic loading of classes
In external pages, there is no need to introduce class files, but the program will automatically "dynamically load" a class when it needs it.
- When creating an object new
- directly use a class name (to operate static properties and methods)
Use the __autoload magic function
When two situations occur, this function will be called. This function requires us to define it in advance and write a general statement for loading class files in it
<code><span><span>function</span><span>__autoload</span><span>(<span>$name</span>)</span>{</span><span>require</span><span>'./lib/'</span>.<span>$name</span>.<span>'.class.php'</span>; }</code>
Use spl_autoload_register()
Use it to register (declare) multiple functions that can replace __autoload(). Naturally, you have to define these functions, and the functions of the functions are the same as __autoload(), but you can deal with more situations at this time
<code><span>//注册用于自动加载的函数</span> spl_autoload_register(<span>"model"</span>); spl_autoload_register(<span>"controll"</span>); <span>//分别定义两个函数</span><span><span>function</span><span>model</span><span>(<span>$name</span>)</span>{</span><span>$file</span> = <span>'./model/'</span>.<span>$name</span>.<span>'.class.php'</span>; <span>if</span>(file_exists(<span>$file</span>)){ <span>require</span><span>'./model/'</span>.<span>$name</span>.<span>'.class.php'</span>; } } <span>//如果需要一个类,但当前页面还没加载该类</span><span>//就会依次调用model()和controll(),直到找到该类文件加载,否则就报错</span><span><span>function</span><span>controll</span><span>(<span>$name</span>)</span>{</span><span>$file</span> = <span>'./controll/'</span>.<span>$name</span>.<span>'.class.php'</span>; <span>if</span>(file_exists(<span>$file</span>)){ <span>require</span><span>'./controll/'</span>.<span>$name</span>.<span>'.class.php'</span>; } }</code>
The above has introduced the automatic loading of PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].
