【问题标题】:PHP Fatal error: Call to undefined function even though function is defined in included filePHP 致命错误:调用未定义的函数,即使函数已在包含的文件中定义
【发布时间】:2018-03-29 11:39:59
【问题描述】:

main.php 需要 2 个文件:

require './functions/fileA.php';
require './functions/fileB.php';

fileA.pgp 作为函数

function doecho($url){echo $url}

在fileB.php中,我有一个调用doecho函数的函数

function withinBfunction($url){doecho($url);}

但我得到了这个错误,

Uncaught Error: Call to undefined function curl().

但是,如果在 fileB.php 中我在任何函数之外调用 doecho 函数,它就可以工作。 似乎fileB.php中的函数无法访问fileA.php中的函数

我看了这篇文章 (PHP Fatal error: Call to undefined function when function is defined) 但不同的是我没有使用类。有什么想法可以解决这个问题吗?

哦,是的,我已经使用 (error_reporting(E_ALL & ~E_NOTICE & E_STRICT & ~E_DEPRECATED) 打开了 php 错误日志,我得到的只是“调用未定义函数”。

【问题讨论】:

  • 如果文件无法加载,include 不会死。使用require 确保文件正确加载。
  • 您确定包含fileA.php 吗?也许解释器找不到它,并且它触发的警告被您的配置所抑制。请改用require
  • 如果 PHP 说它不存在,它不存在,不管你怎么想。
  • 可能是您在include './functions/fileB.php'; 中缺少一个句点。试试include '../functions/fileB.php';
  • 显示错误会对您有所帮助,请在寻求帮助之前尝试调试。 stackoverflow.com/q/5438060/3664960

标签: php function scope


【解决方案1】:

我认为您应该为一个目录使用两个点,例如: 包括'../目录名';

【讨论】:

  • 嗨。不要认为这是问题所在。我现在使用“require”,而不是“include”。两个包含的文件都在同一个文件夹中。如果我放两个点, require 失败。
猜你喜欢
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 2011-05-27
  • 2011-11-11
  • 2020-08-08
相关资源
最近更新 更多