php使用glob方法引入多个文件

2017-5-26 liyangweb.com 李杨 PHP

有时候我们有这样的需求,一个项目的入口处,需要引入多个配置文件,最简单粗暴的方法是这样的。


require(__DIR__.'/config/database.php');
require(__DIR__.'/config/redis.php');
require(__DIR__.'/config/memcache.php');


这样引入自然是可以的,但是有两个问题:


  1. 如果我们增加了配置文件,就需要在这里增加一行代码来引入配置文件。
  2. 如果我们有很多配置文件(或者其他文件),这里就会写很多引入的语句。

PHP给我们提供了一个非常方便的函数glob,这个函数可以返回指定目录下的指定文件。我们这样写:

foreach(glob(__DIR__.'/config/*.php') as $php){
    require_once $php;
}


这样就引入了config目录下所有的php文件。

而且,glob还可以支持更复杂的检索方式,像这样:

foreach(glob(__DIR__.'/config/*/config*.php') as $php){
    require_once $php;
}

这样就引入了config目录下一级目录中的所有config开头的php文件。

标签: PHP

评论:

eyecovet
2019-01-27 12:43
<a href="http://www.gardensbyruth.com/kyrie-3-eybl-for-vendita-virginia-kurpesa">kyrie 3 eybl for vendita virginia</a> <a href="http://www.batemanafrica.com/air-jordan-2-gr%C3%B8n-brun-kurpesc">air jordan 2 gr酶n brun</a> <a href="http://www.tritechems.com/nike-air-max-huarache-oro-obuva">nike air max huarache oro</a> <a href="http://www.malocalguide.com/kvinders-asics-gel-kayano-21-brun-schuhef">kvinders asics gel kayano 21 brun</a> <a href="http://www.indeeprecords.com/nike-flyknit-lunar-3-kvinders-hvid-orange-chaussuresg">nike flyknit lunar 3 kvinders hvid orange</a> <a href="http://www.clearancedept.com/adidas-eqt-ni%C3%B1os-zapatos-verde-negro-obuvd">adidas eqt ni帽os zapatos verde negro</a>
eyecovet http://www.eyecovet.com/
tryautolikes
2019-01-17 20:02
<a href="http://www.printonlypros.com/limited-womens-bruce-ellington-pink-jersey-nfl-nike-houston-texans-12-rush-fashion-nflr">limited womens bruce ellington pink jersey nfl nike houston texans 12 rush fashion</a> <a href="http://www.sklroofing.com/salomon-s-lab-sense-gul-orange-kurpesf">salomon s lab sense gul orange</a> <a href="http://www.ammampart.com/air-jordan-retro-30-cielo-blu-bianca-schuhef">air jordan retro 30 cielo blu bianca</a> <a href="http://www.bandqlocal.com/nike-kd-vi-dc-for-vendita-obuvg">nike kd vi dc for vendita</a> <a href="http://www.cathmanuel.com/nike-free-run-2-herre-alle-sort-l%C3%B8b-sko-jordans-chaussuresa">nike free run 2 herre alle sort l酶b sko jordans</a> <a href="http://www.dailysewansew.com/nike-light-up-zapatos-oro-butya">nike light up zapatos oro</a>
tryautolikes http://www.tryautolikes.com/
priverussia
2018-12-30 11:00
<a href="http://www.evolutionecig.com/bon-march%C3%A9-nike-roshe-flyknit-hommes-blanc-trainersa">bon march茅 nike roshe flyknit hommes blanc</a> <a href="http://www.georginabauer.com/nike-hyperrev-2017-gris-argent-scarpec">nike hyperrev 2017 gris argent</a> <a href="http://www.bootsoffprice.com/nike-free-alle-sort-on-salg-kurpesd">nike free alle sort on salg</a> <a href="http://www.theprintjobs.com/oakley-jupiter-lx-sunglasses-nz-sunglassesg">oakley jupiter lx sunglasses nz</a> <a href="http://www.dbajournal.com/nike-super-bowl-lii-youth-malcom-brown-limited-olivecamo-jersey-new-england-patriots-nfl-90-2017-salute-to-service-nflr">nike super bowl lii youth malcom brown limited olivecamo jersey new england patriots nfl 90 2017 salute to service</a> <a href="http://www.procomedian.com/air-max-womens-2015-skrillex-skoa">air max womens 2015 skrillex</a>
priverussia http://www.priverussia.com/
canditatenext
2018-12-30 02:23
<a href="http://www.rellthewolf.com/air-jordan-28-red-yellow-skob">air jordan 28 red yellow</a> <a href="http://www.fincasagaro.com/adidas-cap-adizero-climacool-hatc">adidas cap adizero climacool</a> <a href="http://www.mushigiftpack.com/kyrie-2-purple-kds-runninge">kyrie 2 purple kds</a> <a href="http://www.glplawyers.com/nike-roshe-two-flyknit-gr%C3%A5-oransje-schuhea">nike roshe two flyknit gr氓 oransje</a> <a href="http://www.canditatenext.com/nike-huarache-ultra-orange-marron-scarpeb">nike huarache ultra orange marron</a> <a href="http://www.ganuocdua.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a>
canditatenext http://www.canditatenext.com/
kiffley
2018-12-30 01:30
<a href="http://www.ebooksandme.com/jordan-13-noir-cuir-chaussuresa">jordan 13 noir cuir</a> <a href="http://www.fresnotxseo.com/nike-free-run-3-5.0-rot-quarter-butya">nike free run 3 5.0 rot quarter</a> <a href="http://www.itsanomadlife.com/nike-hypervenom-phantom-ii-fg-nero-fluorescent-verde-butyd">nike hypervenom phantom ii fg nero fluorescent verde</a> <a href="http://www.myfoxbeaumont.com/nike-air-presto-4d-or-gris-obuvg">nike air presto 4d or gris</a> <a href="http://www.roidezign.com/air-jordan-17-carolina-blu-zone-kurpesa">air jordan 17 carolina blu zone</a> <a href="http://www.terrysammons.com/herren-nike-air-max-tailwind-8-gold-wei%C3%9F-kurpesf">herren nike air max tailwind 8 gold wei脽</a>
[url=http://www.kiffley.com/]kiffley[/url]
divyangshop
2018-12-28 19:52
<a href="http://www.ictbel.com/35-authentic-grant-hill-detroit-pistons-jersey-mens-adidas-nba-purple-throwback-1995-all-nflv">35 authentic grant hill detroit pistons jersey mens adidas nba purple throwback 1995 all</a> <a href="http://www.ioioland.com/nike-air-max-2017.5-dam%C3%A4nner-gold-trainersa">nike air max 2017.5 dam盲nner gold</a> <a href="http://www.sesliesenyurt.com/adidas-neo-lazy-lyser%C3%B8d-s%C3%B8lv-scarped">adidas neo lazy lyser酶d s酶lv</a> <a href="http://www.wethinkjerky.com/air-jordan-retro-2-bianca-blu-rosso-kurpesd">air jordan retro 2 bianca blu rosso</a> <a href="http://www.dadees.com/ray-ban-sunglasses-61017-price-sunglassesd">ray ban sunglasses 61017 price</a> <a href="http://www.pickwickcrc.com/nfl-brice-butler-womens-ash-12-arizona-cardinals-nike-one-color-pullover-hoodie-nflr">nfl brice butler womens ash 12 arizona cardinals nike one color pullover hoodie</a>
divyangshop http://www.divyangshop.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1