php包含其他内容(php includes inside other includes)

所以我的登录检查我分成了两个部分,我尝试将两个不同的文件包括在内。 首先,我将显示原始代码然后显示拆分代码。 我认为我的问题在于包含链接。 我尝试使用绝对路径。 但它似乎失败了。 我的整个应用程序是模块化的,因此我在App结构的其余部分之外设置了全局文件,并根据需要调用文件。 我还想过只通过_autoload()加载函数,但我不知道这会解决我的问题。

<? //Inventory index.php include$_SERVER['DOCUMENT_ROOT'].'/Globals/db_connect.php'; include$_SERVER['DOCUMENT_ROOT'].'/Globals/functions.php'; sec_session_start();if(login_check($mysqli)==true){?> /////Html and or more php code to be executed. Usualy a mix of both. <?}else{ echo ("You are not authorized to access this page, please <a href=\"http://website.com/AUTH/login.html\">login</a>. <br/>");}?>

这就是我要做的事情.... PS我知道我的代码有点难以阅读我试图将其格式化以便于阅读但我可以处理团队比我可以间隔代码更好。 不知道为什么。

<? //Inventory index.php include$_SERVER['DOCUMENT_ROOT'].'/Globals/auth1.php';?> /////Html and or more php code to be executed. Usualy a mix of both. <?}else{include$_SERVER['DOCUMENT_ROOT'].'/Globals/auth2.php';?>

我认为正在发生的是Auth1中的包含失败。 我的绝对路径是否失败? 我最好使用_autoload()。

So my login check I have split into two sections that I tried putting in two different files and including them. First I will show the original code then the split code. I think my problem is with the link to the include. I try to use an absolute path. But it seems to be failing. My whole App is modular so I have global files set up outside of the rest of the App structure and I call the files as needed. I have also thought of just loading the functions through _autoload() but I don't know that this would solve my issue.

<? //Inventory index.php include$_SERVER['DOCUMENT_ROOT'].'/Globals/db_connect.php'; include$_SERVER['DOCUMENT_ROOT'].'/Globals/functions.php'; sec_session_start();if(login_check($mysqli)==true){?> /////Html and or more php code to be executed. Usualy a mix of both. <?}else{ echo ("You are not authorized to access this page, please <a href=\"http://website.com/AUTH/login.html\">login</a>. <br/>");}?>

here is what I am trying to do....P.S. I know my code is kinda hard to read I am trying to format it for easier reading but I can process clumps better than I can spaced code. Not sure why.

<? //Inventory index.php include$_SERVER['DOCUMENT_ROOT'].'/Globals/auth1.php';?> /////Html and or more php code to be executed. Usualy a mix of both. <?}else{include$_SERVER['DOCUMENT_ROOT'].'/Globals/auth2.php';?>

What I think is happening is the includes in Auth1 are failing. are my absolute paths failing? Am I better off using _autoload().

最满意答案

出于多种原因,您应该始终选择“自动加载”以支持手动包含。

可读结构 - 每个文件一个类,每个命名空间段一个文件夹,允许您快速查找项目目录结构中的任何内容。

易于维护 - 如果您更改了文件的文件夹结构/位置(以及它们的命名空间),则无法重写和验证路径。

与其他项目的兼容性,例如使用PSR-0或PSR-4 。 PSR-0用于覆盖遗留代码, PSR-4应用于新库。

与软件包管理软件的兼容性 - 期望遵守某种可预测的约定。 例如, Composer需要PSR-0或PSR-4,它可以为您启用自动加载。

关于编码标准和格式的主题同样适用:

熟悉的结构更容易识别。

这个决定完全是武断的; 它始终是您(或您的团队)的选择。 要么工作正常 - 你会习惯其中任何一个。

一致性很重要, PSR-1和PSR-2是Symfony和朋友以及许多其他使用作曲家作为他们的包管理(和其他人)的项目的通用语言。

You should always choose "autoloading" in favor of manual includes for multiple reasons.

Readable structure -- one class for each file and one folder for each namespace segment allows you to find anything in the directory structure of your project very fast.

Easy maintenance -- if you change the folder structure/position of your files (as well as their namespaces) you're done, no paths to be rewritten and verified.

Compatibility with other projects for example using PSR-0 or PSR-4. PSR-0 is meant for covering legacy code, PSR-4 should be used for new libraries.

Compatibility with package management software -- which expect some kind of predictable conventions to be respected. For example Composer expects either PSR-0 or PSR-4 and it enables autoloads for you.

On the subject of coding standards and formatting the same applies:

familiar structures are easier to recognize.

The decision is perfectly arbitrary; it's always your (or your team') choice. Either will work fine -- you will get used to any one of them.

Consistence is important, PSR-1 and PSR-2 are the lingua franca of both Symfony & friends as well as many other projects which use composer as their package management (and others).

更多推荐

code,_autoload,电脑培训,计算机培训,IT培训"/> <meta name="description&qu