首页 文章

即使在调用get_header()之后,wordpress header.php中包含的文件也无法访问页面模板

提问于
浏览
0

我正在开发一个自定义wordpress模板 . 我有几个页面模板用于布局 . 我分别在页面模板的顶部和底部调用get_header()和get_footer() .

现在的问题是 . 我在header.php文件中使用了两个或三个require_once()来包含php类文件 . 在其中一个包含的文件中,我为包含的类文件创建了一个对象 . 但是当我在我的页面文件中调用这些对象时( - 我使用了get_header() - ),它表示未定义的变量 .

这是我的wordpress header.php

// THIS IS MY header.php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if(session_id() == '')
    session_start(); 

date_default_timezone_set('Asia/Dubai');

require_once('risk-profiler/configuration.php'); // Config file
require_once('risk-profiler/dal.php'); // One class files
require_once('risk-profiler/bl.php'); // another class file
require_once('risk-profiler/form_handler.php'); // Were Objects for the classes are created
?>

form_handler.php

if (!isset($data))
    $data = new DataAccessLayer(sql_server, sql_user, sql_password, sql_database);
if (!isset($bl))
    $bl = new businessLogic;

$ data是数据库类的对象,$ bl是另一个类的对象 .

现在这是我调用 get_header() risk_profile_questionnaire.php 的地方,我在这个文件(表单)中包含两个表单( risk-profile-select-profiling-country.php & another.php ),我调用该对象并且无法访问它 .

risk_profile_questionnaire.php

<div class="form-group">
            <label for="" class="col-md-4 control-label">Country : </label>
            <div class="col-sm-8">
                <select class="form-control input-lg" name="version_details">
                    <?php
                        $version_details = $data->get_version_list();
                        while ($row = mysqli_fetch_array($version_details)) {
                            echo"<option value='" . $row['country_code'] . "|" . $row['version'] . "'>" . $row['country_name'] . "</option>";
                        }
                    ?>
                </select>
            </div>
        </div>

任何人都可以帮助解释为什么我的对象在那时无法访问 .

1 回答

  • 1

    我现在无法测试,但我的猜测是因为变量范围 .

    如果要在PHP中的函数内使用全局变量,则需要在函数开头将其声明为全局变量 .

    由于您从函数“get_header”中包含header.php(以及header.php中包含的其余文件),因此默认情况下变量限制为“get_header”函数的范围 .

    尝试在header.php文件的开头声明需要使用的全局变量,例如:

    global $data;
    

    PHP中的变量范围:http://php.net/manual/en/language.variables.scope.php

相关问题