我是 PHP 新手,在使用 require 时遇到问题。
- 'Config.php' 在父文件夹中
- 模型包含测试,其中 'LaptopModel.php' 是。
- Controller 包含文件“LaptopController.php”
在 Config.php 中:
<?php
$host = "localhost";
$username = "admin";
在 LaptopModel.php 中:
<?php
require '../../Config.php';
在 LaptopController.php 中:
<?php
require '../Model/test/LaptopModel.php';
问题是当服务器处理这个文件时,LaptopController.php 中的代码到底是什么? 我以两种方式考虑这一点,这两种方式是否会导致错误。
<?php
require '../../Config.php';// get the original code from required file
//this cause an error, because from LaptopController.php
//the link to Config.php must be '../Config.php'
或者它将是:
<?php
$host = "localhost";
$username = "admin";
//get what was required from Laptopmodel.php file, not the original code
最佳答案
相对于你的问题部分,Require 真正做的很简单...... 如果文件丢失或找不到它会产生一个 fatal error 并停止脚本的执行
关于php - 在 php 中真正需要做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671230/