__autoload 中是不是一定需要 require_once?
Jun 06, 2016 pm 08:33 PM
看到 __autoload 连官方手册都是 require_once,但是自己测试的时候,如果文件已经加载过了,是不会再执行加载的:
【index.php】
<code><?php function __autoload($className) { echo 'autoload'; require($className.'.php'); } $obj = new sample(); $obj = new sample(); $obj = new sample(); $obj = new sample(); $obj = new sample(); </code></code>
【sample.php】
<code><?php class sample{} </code></code>
index.php 和 sample.php 同目录,事实上最后只输出了一次 'autoload' 字符串。
那这么说来,是不是用 require 就可以了,何必用 once 造成不必要的性能浪费呢?
回复内容:
看到 __autoload 连官方手册都是 require_once,但是自己测试的时候,如果文件已经加载过了,是不会再执行加载的:
【index.php】
<code><?php function __autoload($className) { echo 'autoload'; require($className.'.php'); } $obj = new sample(); $obj = new sample(); $obj = new sample(); $obj = new sample(); $obj = new sample(); </code></code>
【sample.php】
<code><?php class sample{} </code></code>
index.php 和 sample.php 同目录,事实上最后只输出了一次 'autoload' 字符串。
那这么说来,是不是用 require 就可以了,何必用 once 造成不必要的性能浪费呢?
你应该能明白
__autoload
是什么时候触发的吧???
在你第一次触发之后, 你成功的加载了 拥有这个class
的php文件,
那么 当你第二次new
它的时候, 已经存在了这个class
, 就不会再次触发__autoload
了的.官方没有规定说必须使用
require_once
吧? 那个只是例子吧.
http://php.net/manual/zh/function.autoload.php

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein
