php中名称空间问题和对未定义函数的调用
大家好,每当我转到index.php时,我都遇到问题,我得到一个错误:致命错误:在第6行的C:\xampp\htdocs\Blog\Blog.php中调用未定义的函数Blog\DB\connect()。在管理文件夹中,我需要我的blog.php index.phpphp中名称空间问题和对未定义函数的调用,php,namespaces,Php,Namespaces,大家好,每当我转到index.php时,我都遇到问题,我得到一个错误:致命错误:在第6行的C:\xampp\htdocs\Blog\Blog.php中调用未定义的函数Blog\DB\connect()。在管理文件夹中,我需要我的blog.php index.php <?php require '../blog.php'; ?> 现在在blog.php中 <?php require 'functions.php'; require 'db.php
<?php
require '../blog.php';
?>
现在在blog.php中
<?php
require 'functions.php';
require 'db.php';
$conn = Blog\DB\connect($config);
if( !$conn )
{
die('couldnt connect to the database');
}
最终,您需要这样的全局名称空间
$conn = \Blog\DB\connect($config); // mind the \ before Blog
或者该函数只是没有定义(connect()
即)。调用该函数时,您是从blog.php调用函数,而不是从错误指示的db.php调用函数。为了解决这个问题,您必须包含db.php(require可能已经包含了它),然后正常地调用该函数,例如
include db.php
$conn = connect($config)
你的index.php代码在哪里?blog.php是index.php,很抱歉我第一次使用堆栈溢出。如果我没记错,你需要将blog.php命名为index.php,这样才能找到索引?是的,我编辑了它,但我真的不明白为什么它说调用未定义的函数connect()只在那个管理目录中。你调用的函数是错误的,在连接($config)之前去掉整个Blog\DB\并包含DB.php。不,我试过了,我没有工作,抱歉Blog.php是index.php。我做了同样的事情,但它不工作。不知道为什么?不,什么都没有@General Charismo
include db.php
$conn = connect($config)