简体   繁体   English

致命错误:调用未定义的函数

[英]Fatal error: Call to undefined function

I am trying to call a function which is defined in functions.php. 我正在尝试调用在functions.php中定义的函数。 But I can't figure out why this error is coming. 但我不知道为什么会出现此错误。

Let me tell you in detail what I am trying to accomplish: there are two header files — client and admin. 让我详细告诉您我要完成的工作:有两个头文件-客户端和管理。 The client header will be accessed when the user enters into a page through index.php, but admin header will be accessed after an admin successfuly logged in. 当用户通过index.php进入页面时,将访问客户端标头,但是在管理员成功登录后将访问admin标头。

In the functions.php this is the function I have defined: 在functions.php中,这是我定义的功能:

function get_my_header(){
  if(is_home()){ 
    get_header('client'); 
  }
  elseif(is_page('dashboard')){ 
    get_header('own'); 
  }
}

from index.php get_my_header() is called with no error but when I try to access the same function from the admin page I am getting this error: 从index.php的get_my_header()调用没有错误,但是当我尝试从管理页面访问相同的功能时,出现此错误:

Fatal error: Call to undefined function get_my_header() 致命错误:调用未定义的函数get_my_header()

The page where this error is coming has only this one line: 发生此错误的页面只有这一行:

<?php get_my_header(); ?>

The most common causes of this are: 造成这种情况的最常见原因是:

  • The file you think is being included, isn't being included. 您认为包含的文件,不包含的文件。
  • Your calling the function prior to including the file 您在包含文件之前调用函数

Based on your description as a header a file - I think the first is most likely. 根据您对文件标头的描述-我认为第一个很有可能。 As you also mention that it's in an admin page that it doesn't work on - does the admin page live in a sub directory eg: 正如您还提到的那样,它在管理页面中无法使用-管理页面是否位于子目录中,例如:

httpdocs/
   index.php
   admin/
       admin_index.php

If it does - the file path to the include file may be wrong, depending on your error_reporting() settings you may or may not be alerted to this. 如果是这样,则包含文件的文件路径可能错误,具体取决于您的error_reporting()设置,您可能会或可能不会对此有所了解。

One way to debug this would be to use require() instead of include() on the functions file - as that creates a fatal error if PHP can't find the script. 一种调试方法是在函数文件上使用require()而不是include() -因为如果PHP无法找到脚本,则会产生致命错误。

http://php.net/manual/en/function.require.php http://php.net/manual/en/function.require.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM