关于PHP中require的问题

WBOY
Freigeben: 2016-06-23 14:06:03
Original
867 Leute haben es durchsucht

1.使用PHP中require如何包含一个文件夹中的所有文件?
require("./folder/*")是不行的,难道PHP中不能像Java中的include一样灵活方便吗?
不能正则识别名称吗?模式识别?

2。不用require同一个类两次?
file name: A_Dao.class.php 
function: contain basic operations of A.class
A_Dao.class.php
require("DBConnection.class.php");
...
...
...
?>

another file: B_Dao.class.php
function: contain basic operations of B.class
B_Dao.class.php
require("DBConnection.class.php");
...
...
...
?>

A and B.class are entities as abstracted from conceptions.

ServiceA.class.php
require("./dao/A_Dao.class.php");
require("./dao/B_dao.class.php");
...
...
...
?>
run ServiceA.class.php
"Fatal error: Cannot redeclare class DBConnection in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\application\dao\DBConnection.class.php on line 3"

What's wrong? 
为什么不能重复require相同的(DBConnection.class.php)?
编译器不会总动处理吗(像Java一样)?


回复讨论(解决方案)

为什么要require(*)呢?
require会把目标文件全部插入当前文件。如果允许了*,目录下所有文件都会被插入进来,这样,文件会超级大。

第二个问题:
require_once

C/C++都有不允许重复include同一个文件。

php的include和c语言是差不多的,每次执行页面都会include一次,

如果支持这个,我想,会严重影响效率

不像java的import java.io.*;编译后,只导入用得到库,不影响.class大小

require("./folder/*")会加重服务器的负担,因为载如了所有folder下面的文件,Web服务器每次接收请求都这么干的话,服务器是承受不了的。

php只允许包含一次,你可以使用require_once来避免重复require.

就是说PHP不会自动优化一下
但是我针对每一个实体对象都写了一个对应的操纵数据的类
这样我在一个业务中需要处理所有的实体对象,进行数据库的insert和select
实体对象有14个,相应的DAO class有14个
我以前是用java,Structs框架就是这样分层的
不过我现在使用的我称之为MV
folders:
entities: contain all the entity classes
dao: contain all the database access objects according to every entity
service: use dao and entitises to complete tasks
我第一次使用PHP,不知道大家使用PHP都是如何系统分层的?如何设计的?

http://blog.csdn.net/tyjhField
在我的博客里我写了问题,有兴趣的可以帮助、探讨一下

你可以用Autoload

require_once()

1. 相比较而言,Java 可以说是“符号级”的包含,而 PHP 中,无论 include/include_once/require/require_once,都是“文件级”的包含,所以,PHP 不支持通配符也是可以理解的。如果你一定想要“包含一个文件夹下所有程序文件”的效果,可以用 scandir() 或者类似的办法做遍历来包含,也不复杂。只要是“需要的”,无所谓效率不效率。

2. 用 require_once 就好了。1 楼、3 楼兄弟说的都有不准确的地方。PHP/C/C++ 都允许包含多次。PHP 重复包含时经常会报错,只是因为 function 和 class 不能“重复定义”而已。


????????????????????????????????
[img=http://csdnimg.cn/bbs/m/i/red_1.gif]勋章[/img][img=http://csdnimg.cn/bbs/m/i/yellow_1.gif]勋章[/img][img=http://csdnimg.cn/bbs/m/i/blue_1.gif]勋章[/img]勋章啊……我啥时候也能得到这些……
????????????????????????????????
基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具, 分享给大家,欢迎 技术交流 :)

我也搞不懂那个require 和 include 怎么用 ,有哪位大哥能帮我把wordpress和淘客整合一下,求帮助,小弟网址www.nygjp.cn

不懂,不理解,求讲解。。。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!