当我尝试包含 class 时,我在 WordPress 中遇到错误
I'm getting an error in WordPress when I'm try to include a class
我必须在我的 WordPress 中创建一个搜索表单,搜索到数据库中的特定 table。因此,我创建了一个 DAO class,我在其中进行查询并将结果保存到 DTO class 中。为此,我创建了一个摘要 class,我在其中连接到我的数据库,然后进行查询。像这样:
abstract class abstract_dao {
protected $conectorbd;
function __construct() {
$this->conectorbd = new conectorbd();
}
}
我在哪里进行连接,然后我有 cancion_dao.php
,像这样:
define('RUTA', get_bloginfo('template_directory') . "/");
require_once 'abstract_dao.php';
require_once '../db/dto/cancion_dto.php';
class cancion_dao extends abstract_dao {
function __construct() {
parent::__construct();
}
function getCanciones($consulta) {
$sql = "SELECT * FROM canciones WHERE trackName LIKE '%" . $this->conectorbd->escapar_cadena($consulta) . "%'";
$vuelta = $this->conectorbd->ejecutar_query($sql);
$dto = null;
while ($datos = $this->conectorbd->bd_fetch_array($vuelta)) {
$dto = new cancion_dto($datos['id'], $datos['trackName'], $datos['artistName'], $datos['albumName'], $datos['category']);
}
return $dto;
}
}
我知道这不是 100% 正确。但对于我的问题,我认为这已经足够了。有关详细信息,我在同一目录中有 abstract_dao
和 cancion_dao
。我有这个:
wp-content/db
---------conectordb.php
---------/dao
-------------/abstract_dao.php
-------------/cancion_dao.php
---------/dto
-------------/cancion_dto.php
所以,所有这些信息都是为了澄清我的情况。这就是问题所在:
当我尝试搜索某些内容并调用我的函数时 getCanciones()
我收到此错误:
require_once(/db/dto/cancion_dto.php): failed to open stream: No such file or directory on line 4.
然后,我尝试使用完整的 URL:
define('RUTA', get_bloginfo('template_directory') . "/");
我收到这条新消息错误:
require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0
我尝试使用其他选项,例如使用 $_SERVER['DOCUMENT_ROOT']
、dirname( __FILE__ )
等。但我唯一得到的是消息错误。
我不知道如何解决这个问题,希望有人能提供帮助。 如何包含不在同一目录中的文件?
谢谢你,对不起我的英语水平!
您可能需要使用 get_template_directory()
@return string 模板目录路径。
// it will work when you have structure like project.com/wp-content/themes/yourThemeName/db/dto/cancion_dto.php
include_once(get_template_directory()."/db/dto/cancion_dto.php");
// and For use the function from your class
$cancion_Class = new cancion_dao();
$cancion_Class->getCanciones($consulta);
我必须在我的 WordPress 中创建一个搜索表单,搜索到数据库中的特定 table。因此,我创建了一个 DAO class,我在其中进行查询并将结果保存到 DTO class 中。为此,我创建了一个摘要 class,我在其中连接到我的数据库,然后进行查询。像这样:
abstract class abstract_dao {
protected $conectorbd;
function __construct() {
$this->conectorbd = new conectorbd();
}
}
我在哪里进行连接,然后我有 cancion_dao.php
,像这样:
define('RUTA', get_bloginfo('template_directory') . "/");
require_once 'abstract_dao.php';
require_once '../db/dto/cancion_dto.php';
class cancion_dao extends abstract_dao {
function __construct() {
parent::__construct();
}
function getCanciones($consulta) {
$sql = "SELECT * FROM canciones WHERE trackName LIKE '%" . $this->conectorbd->escapar_cadena($consulta) . "%'";
$vuelta = $this->conectorbd->ejecutar_query($sql);
$dto = null;
while ($datos = $this->conectorbd->bd_fetch_array($vuelta)) {
$dto = new cancion_dto($datos['id'], $datos['trackName'], $datos['artistName'], $datos['albumName'], $datos['category']);
}
return $dto;
}
}
我知道这不是 100% 正确。但对于我的问题,我认为这已经足够了。有关详细信息,我在同一目录中有 abstract_dao
和 cancion_dao
。我有这个:
wp-content/db
---------conectordb.php
---------/dao
-------------/abstract_dao.php
-------------/cancion_dao.php
---------/dto
-------------/cancion_dto.php
所以,所有这些信息都是为了澄清我的情况。这就是问题所在:
当我尝试搜索某些内容并调用我的函数时 getCanciones()
我收到此错误:
require_once(/db/dto/cancion_dto.php): failed to open stream: No such file or directory on line 4.
然后,我尝试使用完整的 URL:
define('RUTA', get_bloginfo('template_directory') . "/");
我收到这条新消息错误:
require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0
我尝试使用其他选项,例如使用 $_SERVER['DOCUMENT_ROOT']
、dirname( __FILE__ )
等。但我唯一得到的是消息错误。
我不知道如何解决这个问题,希望有人能提供帮助。 如何包含不在同一目录中的文件?
谢谢你,对不起我的英语水平!
您可能需要使用 get_template_directory()
@return string 模板目录路径。
// it will work when you have structure like project.com/wp-content/themes/yourThemeName/db/dto/cancion_dto.php
include_once(get_template_directory()."/db/dto/cancion_dto.php");
// and For use the function from your class
$cancion_Class = new cancion_dao();
$cancion_Class->getCanciones($consulta);