【问题标题】:I'm getting an error in WordPress when I'm try to include a class当我尝试包含一个类时,我在 WordPress 中遇到错误
【发布时间】:2015-08-06 08:39:58
【问题描述】:

我必须在我的 WordPress 中将搜索表单放入数据库中的特定表中。因此,我创建了一个 DAO 类,在其中进行查询并将结果保存到 DTO 类中。为此,我创建了一个抽象类,在其中连接到我的数据库,然后进行查询。像这样的:

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_daocancion_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__ ) 等。但我得到的唯一结果是消息错误。

我不知道如何解决这个问题,希望有人能提供帮助。 如何包含不在同一目录中的文件?

谢谢你,对我的英语水平感到抱歉!

【问题讨论】:

    标签: php wordpress class include


    【解决方案1】:

    您可能需要使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多