Home
php教程
PHP開發(fā)
YiiBase automatic loading class and reference file method analysis (autoload) in Yii2



YiiBase automatic loading class and reference file method analysis (autoload) in Yii2
Dec 24, 2016 am 09:06 AM本文實(shí)例分析了Yii2中YiiBase自動(dòng)加載類、引用文件的方法。分享給大家供大家參考,具體如下:
在Yii2中這個(gè)函數(shù)是用來(lái)加載類的,沒(méi)有直接引用文件的相關(guān)實(shí)現(xiàn)。但是這個(gè)也可以用來(lái)引用文件。
public static function autoload($className) { /* * $classMap是一個(gè)name-value數(shù)組, * name為類的名稱, * value為這個(gè)類的文件路徑,其中路徑可包含別名 * * 首先在$classMap中查找是否設(shè)置了類別名路徑 */ if (isset(static::$classMap[$className])) { $classFile = static::$classMap[$className]; //如果路徑是別名,獲取別名路徑 if ($classFile[0] === '@') { $classFile = static::getAlias($classFile); } //如果$className以"\"開頭,如:'\a\b\MyClass' //則會(huì)生成別名 '@a/b/MyClass.php',然后獲取對(duì)應(yīng)的別名路徑。 } elseif (strpos($className, '\\') !== false) { $classFile = static::getAlias('@' . str_replace('\\', '/', $className) . '.php', false); if ($classFile === false || !is_file($classFile)) { return; } } else { return; } //引用文件 include($classFile); //如果debug,并且類不存在.... //則異常 //所以 在關(guān)閉debug的情況下 也是可以加載文件的 if (YII_DEBUG && !class_exists($className, false) && !interface_exists($className, false) && !trait_exists($className, false)) { throw new UnknownClassException("Unable to find '$className' in file: $classFile. Namespace missing?"); } }
更多希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

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)