下面描述了关于自动加载器特性强制性要求:
强制性
- 一个完全标准的命名空间必须要有一下的格式结构
\<Vendor Name>\(<Namespace>\)*<Class Name>
- 命名空间必须有一个顶级的组织名称 (“Vendor Name”).
- 命名空间中可以根据情况决定使用多少个子空间
- 命名空间中的分隔符当从文件系统加载的时候将被映射为
DIRECTORY_SEPARATOR
- 命名空间中的类名中的
_
没有特殊含义,也将被作为DIRECTORY_SEPARATOR
对待. - 命名空间中的类名在从文件系统加载时文件名都需要以
.php
结尾 - 组织名,空间名,类名都可以随意选择使用大小写英文字符
示例
\Doctrine\Common\IsolatedClassLoader
=>/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\Symfony\Core\Request
=>/path/to/project/lib/vendor/Symfony/Core/Request.php
\Zend\Acl
=>/path/to/project/lib/vendor/Zend/Acl.php
\Zend\Mail\Message
=>/path/to/project/lib/vendor/Zend/Mail/Message.php
命名空间和类名中的下划线
\namespace\package\Class_Name
=>/path/to/project/lib/vendor/namespace/package/Class/Name.php
\namespace\package_name\Class_Name
=>/path/to/project/lib/vendor/namespace/package_name/Class/Name.php
以上是我们为实现无痛的自动加载特性设定的最低标准。你可以按照此标准实现一个SplClassLoader在PHP 5.3中去加载类。
实例
下面是一个函数实例简单展示如何使用上面建议的标准进行自动加载
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
SplClassLoader实现
下面的gist是一个SplClassLoader实例可以按照上面建议的自动加载特性来加载类。这也是我们当前推荐在PHP5.3中按照上述标准加载类的方式
Pingback: PHP编码规范:基本代码规范 | 郑州PHP.Net
When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the identical comment. Is there any manner you can remove me from that service? Thanks!
It’s an amazing paragraph designed for all the internet people; they will get benefit from it I am sure.
Wonderful blog! Do you have any suggestions for aspiring writers?
I’m planning to start my own site soon but I’m a little lost on everything.
Would you suggest starting with a free platform like WordPress or
go for a paid option? There are so many choices out there that
I’m completely overwhelmed .. Any recommendations? Cheers!
This is really interesting, You’re an excessively professional blogger.
I’ve joined your feed and look forward to seeking
extra of your excellent post. Additionally, I’ve shared
your website in my social networks
Hello, the whole thing is going perfectly here and ofcourse every one is sharing information, that’s truly excellent,
keep up writing.
After checking out a few of the articles on your website,
I honestly like your way of writing a blog. I saved it
to my bookmark site list and will be checking back in the
near future. Take a look at my web site too and tell me what you think.
Nilsxen Jacquard Duvet Cover Set Ophelia & Co.
Hello, this weekend is good for me, for the reason that this occasion i
am reading this fantastic informative post here at my home.
Excellent blog here! Additionally your site so much
up fast! What host are you the usage of? Can I am getting your affiliate hyperlink for your host?
I desire my website loaded up as fast as yours lol
Definitely consider that that you stated. Your favourite justification seemed to be on the
internet the easiest factor to be mindful of. I say to you, I
certainly get irked while people think about worries that they plainly don’t
know about. You controlled to hit the nail upon the highest and
also outlined out the entire thing without having side effect , folks could take a signal.
Will likely be back to get more. Thanks
Always make sure to wear protective devices prior to
servicing them as they are chemically reactive. Use safety glasses and also gloves
for your security. Get rid of the battery from the lorry before testing it.
Use a basic wrench to remove it from the bolts of the battery
owner. Put it on a surface area that is not chemically responsive.
The following step is the preparation of a service required
for carrying out the operation. It is made by mixing
4 ounces of Epsom salt with every 1 quarter of water. Never ever use tap water
as it can be hazardous to the battery cells.
Always make use of distilled water implied for usage in batteries.
As soon as the option is prepared, open up the caps of the cells
and drain pipes the water consisted of in them. After that you need to load
then vacant cell with the prepared service of Epsom salt as
well as distilled water. Use a funnel to make the procedure very easy.
After filling it shake the cells well for even circulation of the option inside the
battery cells.
Following step is to recharge the battery. A 3 phase lead acid battery charger can be utilized for billing it.
Make sure the battery charger remains in OFF setting before you connect the battery to the charger.
After effectively connecting it transform the charger ON
as well as leave it over night for billing.
This piece of writing will help the internet viewers for building up new blog or even a blog from start to end.
Hi friends, its great paragraph regarding cultureand completely defined, keep it up all the time.
Hello, all is going well here and ofcourse every one
is sharing facts, that’s in fact fine, keep up writing.
Great delivery. Great arguments. Keep up the amazing work.
homepage online bets
fantastic issues altogether, you jus received a new reader.
What could yoou suggest inn regasrds to your publish that you simply made
some day in the past? Any sure?
homepage
Amazing things here. I am very glad to peer your post. Thanks so much and I’m looking forward to
contact you. Will you kindly drop me a mail?
I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this hike.
Hello, I would like to subscribe for this website to take most up-to-date updates, so where can i do it please help out.
There is definately a lot to find out about this topic.
I really like all of the points you’ve made.
This article will assist the internet people for building up new blog or even a weblog from stwrt to end.
website