register与auto两个名词可以连用吗吗

的海词问答和网友补充:
相关词典网站:1、安装WP-AutoPost
您可以通过从您的WP-AutoPost.ORG帐户下载ZIP文件手动安装WP-AutoPost。
如果您想手动安装WP-AutoPost,请按照下列步骤操作:
登录您的WP-AutoPost.ORG帐户。
转到“账户”部分。
下载您需要的WP-AutoPost插件(ZIP 文件)。
将下载的 ZIP 文件上传到您网站的’wp-contents/plugins’目录。
解压该ZIP 文件。这将创建一个目录。
登录到您的网站,转到“插件”,并启用它。
2、创建任务及基本设置
点击“新建任务”后,输入任务名称,即可创建新任务,创建好新任务之后可以在任务列表中查看到该任务,就可对该任务进行更多设置。
点击 “设置” 之后,可在 “基本设置选项卡” 下,进行如下设置:
任务名称:修改任务名称
分类目录:该任务采集文章发布到的分类目录
作者:该任务采集文章的发布作者,必须是wordpress里的注册用户
更新时间间隔:间隔多长时间检测一次该采集任务下是否有新文章可以更新
字符集:采集目标网站的字符集编码,默认为UTF8,如果目标网页字符集编码不是UTF8,抓取的网页会出现乱码,设置正确的字符集即可解决该问题(如何正确设置字符集)
下载远程图片:如果该任务下采集的文章里包含图片,可以选择是否下载远程图片到本地服务器,选择下载远程图片可以做以下更多设置:
将下载的图片保存到wordpress媒体库中
将图像保存到 Flickr
将图像保存到七牛云存储
将下载的第一张图片自动设为特色图片
下载的图片自动添加水印,可添加文本水印或图片水印
选择设置图片地址的属性 (如果包含源图片地址的属性不是src,可以在这里做设置)
下载远程附件:如果该任务下采集的文章里包含其他类型附件,可以选择是否下载这些附件文件到本地服务器,选择下载后可以做以下更多设置:
将下载的附件信息保存到wordpress媒体库中
自动标签:选择是否使用自动标签
标签列表:使用自动标签后如果文章包含列表里的关键词,将自动添加标签
匹配完整的单词:该设置对英文文章有效,中文文章请勿启用该设置
自动摘要:可自动将文章中第一段或前面几段文字设为摘要
发表状态:采集发布后文章的状态,可以是:已发布,草稿,等待复审
手动选择性采集:选择是,将手动选择哪些文章可以采集并发布
3、文章来源设置
文章来源设置
在该选项卡下我们需要设置文章来源的 文章列表网址 及 具体文章的匹配规则
我们以采集”新浪互联网新闻“为例,文章列表网址为http://roll..cn/internet_worldlist/index.shtml
因此在 手工指定文章列表网址 中输入该网址即可,如下所示:
之后需要设置该文章列表网址下具体文章网址的匹配规则
文章网址匹配规则
文章网址匹配规则的设置非常简单,无需复杂设置,提供两种匹配模式,可以使用URL通配符匹配,也可以使用CSS选择器进行匹配,通常使用URL通配符匹配较为简单,但有时使用CSS选择器更为精确。
1.使用URL通配符匹配
通过点击列表网址 http://roll..cn/internet_worldlist/index.shtml 上的文章,我们可以发现每篇文章的URL都为如下结构
.cn/i//.shtml
因此将URL中变化的数字或字母替换为通配符
即可,如:
.cn/i/(*)/(*).shtml
2.使用CSS选择器进行匹配
使用CSS选择器进行匹配,我们只需要设置文章网址的CSS选择器即可,通过查看列表网址http://roll..cn/internet_worldlist/index.shtml的源代码即可轻松设置,找到列表网址下文章超链接的代码,如下所示:
可以看到,文章的超链接A标签在class为“contList”的标签内部,因此文章网址的CSS选择器只需要设置为
.contList a
即可,如下所示:
不知道CSS选择器为何物,一分钟学会如何设置CSS选择器
设置完成之后,不知道设置是否正确,可以点击上图中的测试按钮,如果设置正确,将列出该列表网址下所有文章名称和对应的网页地址,如下所示:
4、文章抓取设置
文章抓取设置
在该选项卡下, 我们需要设置文章标题和文章内容的匹配规则,提供两种方式进行设置,推荐使用CSS选择器方式,使用该方式更为简单,精确。(不知道CSS选择器为何物,一分钟学会如何设置CSS选择器)
我们只需要设置文章标题CSS选择器和文章内容CSS选择器,即可准确抓取文章标题和文章内容。
在文章来源设置里,我们以采集”新浪互联网新闻“为例,这里还是以该例子讲解,通过查看列表网址http://roll..cn/internet_worldlist/index.shtml下某一篇文章的源代码即可轻松设置,例如,我们通过查看某篇具体文章.cn/i//.shtml的源代码,如下所示:
可以看到,文章标题在id为“artibodyTitle”的标签内部,因此文章标题CSS选择器只需要设置为
#artibodyTitle
同样的,找到文章内容的相关代码:
可以看到,文章内容在id为“artibody”的标签内部,因此文章内容CSS选择器只需要设置为
即可;如下所示:
设置完成之后,可点击测试按钮,输入测试地址,如果设置正确,将显示出文章标题和文章内容,方便检查设置是否正确。
5、抓取文章分页内容
抓取文章分页内容
如果文章内容过长,有多个分页同样可以抓取全部内容,这时需要设置文章分页链接CSS选择器,通过查看具体文章网址源代码,找到分页链接的地方,例如某篇文章分页链接代码如下:
可以看到,分页链接A标签在class为“page-link”的标签内部,因此,文章分页链接CSS选择器设置为 .page-link a 即可,如下所示:
如果勾选 当发表时也分页 时,发表文章也将同样被分页,如果你的wordpress主题不支持 &!- -nextpage- -& 标签,请勿勾选。
6、内容过滤
文章内容过滤功能,可过滤掉正文中不希望发布的内容(如广告代码,版权信息等),可以使用CSS选择器过滤内容,也可以可设置两个关键词,删除掉两个关键词之间的内容,关键词2可以为空,表示删除掉关键词1之后的所有内容。
使用关键词过滤,如果设置不合适,可能会破坏html的结构,导致页面排版出错。如果可以用CSS选择器过滤内容,推荐使用CSS选择器方式,简单、精确、不会出错。
如下所示,我们通过测试抓取文章后发现文章里有不希望发布的内容,切换到HTML显示,找到该内容的html代码,可以使用CSS选择器,也可以分别设置两个关键词即可过滤掉该内容。
1.使用CSS选择器方式
2.使用关键词方式
如果需要过滤掉多处内容,可以添加多组设置。
7、HTML标签过滤
HTML标签过滤
支持HTML标签过滤功能,可过滤掉采集文章中的超链接,&script&和&style&等标签下不必要的代码。
如果只过滤掉文章中的超链接,只需输入 a 即可,是否删除标签内容选择否;
如果要过滤掉文章中包含的&script&或&style&不必要的代码,只需输入对应标签名称,是否删除标签内容选择是;
8、关键词替换
关键词替换
支持正文标题关键词替换,支持正文自定义链接;
例如:需要将文章里的wordpress自动添加链接到http://wordpress.org/,只需设置wordpress 替换为 &a href=”http://wordpress.org/”&wordpress&/a&
9、添加自定义链接
Auto Link 可以在发布文章的时候自动添加上关键词链接,也可以对已经发布的内容增加关键词链接
10、解决乱码问题
为什么产生乱码
WP-AutoPost 采集网页时默认字符集为UTF-8,如果目标网页的字符集不是UTF-8,采集的内容可能会出现乱码。
解决乱码问题
解决方法非常简单,只需查看目标网页的字符集,设置正确的字符集即可。
打开目标网页源代码,一般在最前面的位置,找到有charset的代码部分:
如上所示,该网页的目标编码为gb2312,设置相同的字符集即可:
11、如何设置CSS选择器
要使用WP-AutoPost设置采集规则,只需要掌握基本的CSS选择器知识即可。
如需学习更多可以参考/cssref/css_selectors.asp
基础选择器
选择器示例
标签选择器,匹配所有使用&h1&标签的元素
标签选择器,匹配所有使用&a&标签的元素
class选择器,匹配所有class属性等于info的元素
id选择器,匹配所有id属性等于footer的元素
class选择器前面有 . 号, 如 .some_word
id选择器使前面有 # 号, 如 #some_word
例如某篇文章的标题HTML代码如下:
因此该文章标题的CSS选择器就是
.entry-title
该文章标题的CSS选择器就是
#artibodyTitle
例如某篇文章的正文内容HTML代码如下:
该文章内容的CSS选择器就是
后代元素选择器
选择器示例
匹配所有在&div&元素内部的&p&标签元素
匹配所有在&li&元素内部的&a&标签元素,并且&li&元素又在&div&元素内部
匹配所有在 class属性等于contList元素内部 的&a&标签元素
匹配所有在 id属性等于contList元素内部 的&a&标签元素
匹配所有在 &li&元素内部的&a&标签元素,并且&li&元素又在 id属性等于contList元素内部
后代元素选择器,每个元素之间用空格隔开
例如要设置文章来源选项卡里的文章网址CSS选择器,文章列表网址里每篇文章超链接HTML代码如下:
因为每篇文章的网址(a 标签)都在class=“contList”的元素内部,所以该文章网址CSS选择器可以设置为
在上面例子中因为&a&标签也在&li&标签内部,因此也可以设置为 .contList
同理&li&标签又在&ul&标签内部,因此也可以设置为 .contList
以上3个设置都是等价的。
12、如何申请微软翻译客户端密钥?
1. 创建 Windows Azure Marketplace 帐户
首先访问 / 注册 Windows Azure Marketplace 帐户,如果有微软的账户(、、、),使用微软账户登陆即可注册即可。
2. 订阅 Microsoft Translator
访问 /dataset/bing/microsofttranslator 订阅 Microsoft Translator
3. 注册应用程序
访问 /developer/applications/register 注册你的应用程序,获取 客户端ID 和 客户端密钥。
使用获取的 客户端ID 和 客户端密钥 填入插件中对应的设置页面即可。
如果每月2,000,000字符的免费翻译数量不够用的话,可以重复申请多组密钥即可,插件可支持设置多组密钥,如果设置多组密钥,使用过程中会随机调用密钥,如果某个密钥无法翻译成功,会尝试使用其他密钥,提高稳定性。
13、如何申请百度翻译API Key?
1. 注册百度开发者账号
首先登陆 /,注册百度账户,如已有百度账户,直接登陆即可。
普通用户登陆之后,注册为百度开发者账户,如图所示,之后按要求填写相关信息提交即可注册成为开发者账户。
2. 成功注册为开发者账户之后,即可创建应用获得API Key
开发者注册成功之后,访问页面 /, 进入开发者服务管理页面。
之后,创建一个工程,如下所示:
随意填写应用的名称,如:My-Test-API
之后即能获得一个API Key,如下所示:
14、如何获取并设置Cookie采集需要登录才能浏览的内容?
如果要采集需要登录后才能浏览的内容,那么就需要设置cookie,获取和设置cookie非常简单。
获取cookie我们需要用到下面这个绿色软件 WebCookiesSniffer。
点击下载 WebCookiesSniffer.zip
解压之后打开 WebCookiesSniffer界面,点击开始的那个箭头。
之后在浏览器中访问已经登录好的网站,在 WebCookiesSniffer 中找到该网站的路径,选中之后点击鼠标右键打开选项菜单,点击打开 Properties窗口。
之后在 Properties 窗口下复制 Cookies String的内容 到插件cookie设置处保存即可。
保存之后,可以进行测试,输入需要登录才能浏览的URL,如果cookie设置正确,测试之后就能看到需要登录之后才能浏览的内容。
关于本站张家口阿玮工作室致力于网络推广、网站建设服务。好的服务为客户带来更高的收益。Keyboard Shortcuts?
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search(current page)
Focus search box
Change language:
Brazilian Portuguese
Chinese (Simplified)
spl_autoload_register
spl_autoload_register & 注册给定的函数作为 __autoload 的实现
bool spl_autoload_register
$autoload_function
[, bool $throw = true
[, bool $prepend = false
如果在你的程序中已经实现了函数,它必须显式注册到队列中。因为
spl_autoload_register()函数会将Zend Engine中的函数取代为或。
如果需要多条 autoload 函数,spl_autoload_register()
满足了此类需求。
它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下,
只可以定义一次。
autoload_function
欲注册的自动装载函数。如果没有提供任何参数,则自动注册 autoload 的默认实现函数。
此参数设置了 autoload_function 无法成功注册时,
spl_autoload_register()是否抛出异常。
如果是 true,spl_autoload_register() 会添加函数到队列之首,而不是队列尾部。
成功时返回 TRUE, 或者在失败时返回 FALSE。
Example #1 spl_autoload_register() 作为
函数的替代
&?php//&function&__autoload($class)&{//&&&&&include&'classes/'&.&$class&.&'.class.php';//&}function&my_autoloader($class)&{&&&&include&'classes/'&.&$class&.&'.class.php';}spl_autoload_register('my_autoloader');//&或者,自&PHP&5.3.0&起可以使用一个匿名函数spl_autoload_register(function&($class)&{&&&&include&'classes/'&.&$class&.&'.class.php';});?&
Example #2 class 未能加载的 spl_autoload_register() 例子
&?phpnamespace&Foobar;class&Foo&{&&&&static&public&function&test($name)&{&&&&&&&&print&'[['.&$name&.']]';&&&&}}spl_autoload_register(__NAMESPACE__&.'\Foo::test');&//&自&PHP&5.3.0&起new&InexistentClass;?&
以上例程的输出类似于:
[[Foobar\InexistentClass]]
Fatal error: Class 'Foobar\InexistentClass' not found in ...
- 尝试加载未定义的类
Good news for PHP 5.3 users with namespaced classes:When you create a subfolder structure matching the namespaces of the containing classes, you will never even have to define an autoloader.&?php& & spl_autoload_extensions(".php"); spl_autoload_register();?&It is recommended to use only one extension for all classes. PHP (more exactly spl_autoload) does the rest for you and is even quicker than a semantically equal self-defined autoload function like this one:&?php& & function my_autoload ($pClassName) {& & & & include(__DIR__ . "/" . $pClassName . ".php");& & }& & spl_autoload_register("my_autoload");?&I compared them with the following setting: There are 10 folders, each having 10 subfolders, each having 10 subfolders, each containing 10 classes.To load and instantiate these 1000 classes (parameterless no-action constructor), the user-definded autoload function approach took 50ms longer in average than the spl_autoload function in a series of 10 command-line calls for each approach.I made this benchmark to ensure that I don't recommend something that could be called "nice, but slow" later.Best regards,
When switching from using __autoload() to using spl_autoload_register keep in mind that deserialization of the session can trigger class lookups.This works as expected: &?phpsession_start();function __autoload($class) {...}?&This will result in "__PHP_Incomplete_Class_Name" errors when using classes deserialized from the session.&?phpsession_start();function customAutoloader($class) {...}spl_autoload_register("customAutoloader");?&So you need to make sure the spl_autoload_register is done BEFORE session_start() is called.CORRECT:&?phpfunction customAutoloader($class) {...}spl_autoload_register("customAutoloader");session_start();?&
Think twice about throwing an exception from a registered autoloader.If you have multiple autoloaders registered, and one (or more) throws an exception before a later autoloader loads the class, stacked exceptions are thrown (and must be caught) even though the class was loaded successfully.
When using spl_autoload_register() with class methods, it might seem that it can use only public methods, though it can use private/protected methods as well, if registered from inside the class:&?php& & class ClassAutoloader {& & & & public function __construct() {& & & & & & spl_autoload_register(array($this, 'loader'));& & & & }& & & & private function loader($className) {& & & & & & echo 'Trying to load ', $className, ' via ', __METHOD__, "()\n";& & & & & & include $className . '.php';& & & & }& & }& & $autoloader = new ClassAutoloader();& & $obj = new Class1();& & $obj = new Class2();?&Output:--------Trying to load Class1 via ClassAutoloader::loader()Class1::__construct()Trying to load Class2 via ClassAutoloader::loader()Class2::__construct()
What I said here previously is only true on Windows. The built-in default autoloader that is registered when you call spl_autoload_register() without any arguments simply adds the qualified class name plus the registered file extension (.php) to each of the include paths and tries to include that file.Example (on Windows):include paths:- "."- "d:/projects/phplib"qualified class name to load:network\http\rest\ResourceHere's what happens:PHP tries to load'.\\network\\http\\rest\\Resource.php'-& file not foundPHP tries to load'd:/projects/phplib\\network\\http\\rest\\Resource.php'-& file found and includedNote the slashes and backslashes in the file path. On Windows this works perfectly, but on a Linux machine, the backslashes won't work and additionally the file names are case-sensitive.That's why on Linux the quick-and-easy way would be to convert these qualified class names to slashes and to lowercase and pass them to the built-in autoloader like so:&?phpspl_autoload_register(& function ($pClassName) {& & spl_autoload(strtolower(str_replace("\\", "/", $pClassName)));& });?&But this means, you have to save all your classes with lowercase file names. Otherwise, if you omit the strtolower call, you have to use the class names exactly as specified by the file name, which can be annoying for class names that are defined with non-straightforward case like e. g. XMLHttpRequest.I prefer the lowercase approach, because it is easier to use and the file name conversion can be done automatically on deploying.
If your autoload function is a class method, you can call spl_autoload_register with an array specifying the class and the method to run.* You can use a static method :&?phpclass MyClass {& public static function autoload($className) {& & }}spl_autoload_register(array('MyClass', 'autoload'));?&* Or you can use an instance :&?phpclass MyClass {& public function autoload($className) {& & }}$instance = new MyClass();spl_autoload_register(array($instance, 'autoload'));?&
A note on registering autoloading functions with additional parameters../alf.home.php&?phpclass ALF {& public function haaahaaahaaa($class = "ALF", $param = "Melmac") {& & echo "I am ".$class." from ".$param.".\n";& }}?&./kate.melmac.php&?phprequire_once("alf.home.php");$alf = new ALF();spl_autoload_register(array($alf,'haaahaaahaaa'));$alf-&haaahaaahaaa(); @$kate = new Kate(); ?&I am ALF from Melmac.I am Kate from Melmac../kate.earth.php&?phprequire_once("alf.home.php");spl_autoload_register(function($class){ & call_user_func(array(new ALF(),'haaahaaahaaa'), $class, "Earth"); });@$kate = new Kate(); ?&I am Kate from Earth.
Be careful using this function on case sensitive file systems.&?phpspl_autoload_extensions('.php');spl_autoload_register();?&I develop on OS X and everything was working fine. But when releasing to my linux server, none of my class files were loading. I had to lowercase all my filenames, because calling a class "DatabaseObject" would try including "databaseobject.php", instead of "DatabaseObject.php"I think i'll go back to using the slower __autoload() function, just so i can keep my class files readable
It seems, that& spl_autoload tests, if the class exists, after calling every registered loader. So it breaks the chain, if the class exists and will not call the other loaders
&?php
function a ($c) {
& echo "a\n";
& class Bla {} }
function b ($c) {
& echo "b\n";
}
spl_autoload_register('a');
spl_autoload_register('b');
$c = new Bla();
?&
Editorial note: The appropriate PHP bug that requests behavior this function emulates is
. This function does NOT work if there has been an array($obj, 'nonStaticMethod') registered in the autoload stack--while the autoload will be removed, it will be re-registered incorrectly.
The spl_autoload_register() method registers functions in its stack in the order that spl_autoload_register() was called, and subsequently if you want an autoload function to override previous autoload functions you will either need to unregister the previous ones or change the order of the autoload stack.
For example, say in your default implementation of an autoload function you throw an exception if the class cannot be found, or perhaps a fatal error.& Later on in your code you add a second implementation of an autoload function which will load a library that the previous method would fail on.& This will not call the second autoloader method first, but rather will continue to error out on the first method.
As previously mentioned, you can unregister the existing autoloader that errors out, or you can create a mechanism for unregistering and re-registering the autoloaders in the order you want.
Here is a sample/example of how you might consider re-registering autoloaders so that the newest autoloader is called first, and the oldest last:
function spl_autoload_preregister( $autoload ) {
& & if ( ($funcs = spl_autoload_functions()) === false ) {
& & & & spl_autoload_register($autoload);
& & } else {
& & & & $compat =
& & & & & & version_compare(PHP_VERSION, '5.1.2', '&=') &&
& & & & & & version_compare(PHP_VERSION, '5.1.0', '&=');
& & & & foreach ($funcs as $func) {
& & & & & & if (is_array($func)) {
& & & & & & & & $reflector = new ReflectionMethod($func[0], $func[1]);
& & & & & & & & if (!$reflector-&isStatic()) {
& & & & & & & & & & throw new Exception('
& & & & & & & & & & & & This function is not compatible
& & & & & & & & & & & & with non-static object methods due to PHP Bug #44144.
& & & & & & & & & & ');
& & & & & & & & }
& & & & & & & & if ($compat) $func = implode('::', $func);
& & & & & & }
& & & & & & spl_autoload_unregister($func);
& & & & }
& & & &
& & & & spl_autoload_register($autoload);
& & & &
& & & & foreach ($funcs as $func) {
& & & & & & spl_autoload_register($func);
& & & & }
& & }
}
Note: I have not tested this for overhead, so I am not 100% sure what the performance implication of the above example are.
It is never a good idea and a unconscienable concept to create the classes in the autoload function via eval. It should be a nice feature with these Exception, but i think anyone is able to handle it without this method although. Atm i dont realize for what this is good for...As i might note, class_exists() will ever define the classes u only want to check for existance, and will therefor ever return true:&?phpfunction EvalIsEvil($class) {& eval('class '.$className.'{}');}spl_autoload_register('EvalIsEvil');if (class_exists($s="IsMyModuleHere")) {& return new $s();}?&
It is important to note that the autoloader will NOT be called if an E_STRICT error triggers the error handler which, in turn, tries to use classes which are not yet loaded. In this instance, you should manually load classes required by the error handler.
&?php& & spl_autoload_register(function($class_name) {& & & & $dirs = array(& & & & & & 'project/', 'classes/', 'tests/',&& );& & & & foreach( $dirs as $dir ) {& & & & & & if (file_exists($dir.'class.'.strtolower($class_name).'.php')) {& & & & & & & & require_once($dir.'class.'.strtolower($class_name).'.php');& & & & & & & && & & & & & }& & & & }& & });
This function is smart enough not to add the same loader twice.& This seems to work for all of the different loader formats.& Example:&?phpclass ALoader{& static function load($class) { return true; }}function anotherLoader($class) {& return true;}$F = new ALoader;spl_autoload_register(array('ALoader', 'load'));spl_autoload_register(array('ALoader', 'load'));spl_autoload_register(array($F, 'load'));spl_autoload_register('anotherLoader');spl_autoload_register('anotherLoader');var_dump(spl_autoload_functions());?&
Looks like on the latest update of debian php passing no params to spl_autoload doesn't work on linux any more.It is failing to replace the \'s in the namespace with /'s for file paths.
Cleverly - and usefully - I have noticed that (on PHP 5.3 at least) these autoloaders "kick in" even when you call a public static method of an as-yet-unloaded all static class.
if(!defined('BASE_PATH')) {& & define('BASE_PATH', dirname(__FILE__) . '/');& & require BASE_PATH . 'Autoloader.php';& & Autoloader::Register();}class Autoloader{& & public static function Register() {& & & & return spl_autoload_register(array('Autoloader', 'Load'));& & }& & public static function Load($strObjectName) {& & & & if(class_exists($strObjectName) === false) {& & & & & && & & & }& & & & $strObjectFilePath = BASE_PATH . $strObjectName . '.php';& & & & & & & & if((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {& & & & & && & & & }& & & & & & & & require($strObjectFilePath);& & }}
spl_autoload_register() can be used with include_path.suppose in current directory we have directory "a", and inside there is directory "test" and inside is test.php :&?class test{& & & & function __construct(){& & & & & & & & echo "Test created\n";& & & & }}?& then we can use following code to load the class:&?ini_set("include_path", "./a/");spl_autoload_register();$t = new \test\test();?&
This autoload will find every class you call if you put each one in a different file.It walks into every directory recursivly from the root you specify in the $root var.You can specify the folders ou don't want to walk in (e.g you won't find any class in a 'view' folder on an MVC project) in the $dir_to_not_look_spl_autoload_register(function($class) {& & $root = 'my/root/path';& & $file = $class . '.php';& & $dir_to_not_look_in = array($directories, $to, $not, $look, $in);& & if(!function_exists('load')) {& & & function load($dir, $file) {& & & & & & if(file_exists($dir . '/' . $file)) {& & & & & & & & require_once $dir . '/' . $& & & & & & } else {& & & & & & & & foreach(scandir($dir) as $value) {& & & & & & & & & & if(is_dir($dir. '/' . $value) && !in_array($value, $dir_to_no_look_in))& & & & & & & & & & & & load($dir. '/' . $value, $file);& & & & & & & & }& & & & & & }& & & & };& & & }& & & & load($root, $file);& & });
if you have a dir-structure like "/abc/def/ghi", your index.php lies in the top directory, but you want to use namespaces starting with "def" or "ghi":
you can switch the namespace root directory of php with e.g. set_include_path(__DIR__ . '/abc') and afterwards define + use your namespaces with the simple spl_autoload_register() function without any arguments supplied.
remember that php handlers "cli" and "cli-server" are special cases.
You can also use it like this:& spl_autoload_register ( array( new AutoloaderClass, 'method') );or in PHP & 5.3:& spl_autoload_register ( [ new My\Namespace\Autoloader, 'method'] );On this way you dont have to create a variable, which is used once.
Note that when specifying the third parameter (prepend), the function will fail badly in PHP 5.2
I've made a little function that makes and registers a loader that seems to be safe and reliable although I'm not sure but it feels like a good idea to share, it took me some time to come up with it I hope it saves someone some time, &?phpfunction Loader($root = "") {& & $loaderFunction = create_function('$class', 'include& "' . $root . '$class.php";');& & spl_autoload_register($loaderFunction);}?&if you have a file system exactly like your directory tree this function works perfectly, I haven't tested it on unix, but on windows, the default loader fails when your webpage isn't in the root directory, this makes sure that it won't cause a problem if your webpage is on a subdir too just pass ../ or more drived ../../ as root and it will work like a charm, note that i couldn't use anonymous functions because then the $root variable wouldn't have the same scope as the Loader function, so the function must be created on the fly. this is a good example of this functions usageyour class:root/classes/support/classic.php&?php& & namespace classes/support;& & class classic {& & & && }?&root/support/index.php&?phpLoader('../');use classes/support/classic;$cls = new classic();?&the loader will make a function like this:&?phpfunction($class) {& & include& "../$class.php";}include '../classes/support/classic.php';?&h
// Get last class version & & if( !function_exists('classAutoLoader') )& & {& & & & function classAutoLoader($className)& & & & {& & & & & & $classFiles& && = array() ;& & & & & & $classFile& && = FALSE ;& & & & & & $classDir& & & & = __DIR__ .'/'& ;& & & & & & // Get all files& & & & & & $files& & & && = scandir( $classDir ) ;& & & & & & foreach ( $files as $url )& & & & & & {& & & & & & & & // Verifie if file is : Name + class + version + extension& & & & & & & & if( is_file($classDir.$url) && preg_match('/([^\.]+)\.class\.([\d\.]+)\.(.+)$/', $url, $m ) )& & & & & & & & {& & & & & & & & & & & & // Verifie finded file& & & & & & & & & & if( $className == $m[1] )& & & & & & & & & & {& & & & & & & & & & & & // Get previous version or init& & & & & & & & & & & & $previous = isset( $classFiles[$m[1]] ) ? $classFiles[$m[1]] : 0 ;& & & & & & & & & & & & & & & & & & & & & & & & // Get last version& & & & & & & & & & & & if( $previous&$m[2] )& & & & & & & & & & & & {& & & & & & & & & & & & & & $classFiles[ $m[1] ] = $m[2] ; // version& & & & & & & & & & & & & & $classFile = $classDir.$& & & & & & & & & & & & }& & & & & & & & & & }& & & & & & & & & & }& & & & & & } ;& & & & & & if( !class_exists($className) ) include( $classFile );& & & & }& & }& & spl_autoload_register('classAutoLoader') ;}

我要回帖

更多关于 as和so可以连用吗 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信