如何访问一级深层文件中的对象?

Posted

技术标签:

【中文标题】如何访问一级深层文件中的对象?【英文标题】:How can I access an object in one level deep file? 【发布时间】:2014-05-09 16:27:06 【问题描述】:

在我的 header.php 文件中,我写道:

 <?php require_once('includes/session.php');?>

在我的 admin_login.php 文件中我写道:

 get_header(); 
 global $session;
 $session->login($found_user);    

get_header() 将包括header.php,其中有'session' 类by&lt;?php require_once('includes/session.php');?&gt;

admin_login.php 通过 get_header() 获得 header.php。

那么为什么我的 admin_login.php 中没有会话类?它显示以下错误:

Call to a member function is_logged_in() on a non-object in 
D:\server\htdocs\wordpress\wp-content\themes\amrajegeachi.com\super_admin_home.php on line 9

admin_login.php 中的所有代码:

<?php 
  /*
    Template Name: admin login form
  */
?>

<?php 
require_once('includes/user.php');
get_header(); // includes.session.php has been included by header.php

?> 
      <div id="super-admin-login">              
  <?php 
if(isset($_POST['login-btn']))

    if(!empty($_POST['user']) || !empty($_POST['password']))
      
          $username=$_POST['user'];
          $password=$_POST['password']; 
          $found_user = User::authenticate($username,$password);
          if($found_user)
              global $session;
               $session->login($found_user);   
            
      

?>
  <form name="input" action="#" method="post">
    <table>
      <tr><td> Username:</td><td><input type="text" name="user" /></td> </tr>
      <tr><td> Password:</td><td><input type="password" name="password" /></td> </tr>
      <tr><td> </td><td class="btn"> <input type="submit" name="login-btn" value="Login" /></td></tr>
    </table> 
</form>
      </div>       
   <?php  
    get_footer();
    ?>

【问题讨论】:

如何以及何时初始化$session var? $session 在 session.php 初始化 当然可以,但具体如何?另外,global $session 是在函数内使用还是在全局范围内使用?如果是后者,那就有点无意义了;你至少要在调用get_header()之前之前定义它。 我在原始问题中添加了代码。简而言之 session.php 如下: class Session // 成员函数声明放在这里 $session = new Session(); $message = $session->message(); 改写global $session = new Session(); 会发生什么?看起来该文件包含在函数范围内,因此存在问题。 【参考方案1】:

我已修复此错误。我将 getheader() 更改为 require_once("header.php");现在可以正常使用了

【讨论】:

以上是关于如何访问一级深层文件中的对象?的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate一级缓存

Hibernate的一级缓存

Hibernate中的一级缓存二级缓存和懒加载

HTMLHTML 标签 ④ ( 文件路径 | 相对路径 | 绝对路径 | 同级目录 | 下一级目录 | 上一级目录 )

课时10:MyBatis一级缓存二级缓存

hibernate一级缓存