定义变量时未定义的变量错误

时间:2013-03-18 16:16:58

标签: php object

好的,就是这样。

<?php
if ($Check->loggedIn()){
    include 'includes/modules/logged_in.mod.php';
} else{
    include 'includes/modules/login.mod.php';
}
?>

它会出现以下错误:

  

注意:未定义的变量:在第8行检入C:\ wamp \ www \ Projects \ Planet Ruin v4 \ includes \ navbar.inc.php

     

致命错误:在第8行的C:\ wamp \ www \ Projects \ Planet Ruin v4 \ includes \ navbar.inc.php中的非对象上调用成员函数loggedIn()

现在,当我对$Check变量执行var_dump时,我得到了这个:(请参阅下面的正确var_dump结果)

object(content)[9]
 protected 'Check' => 
  object(check)[3]
   protected 'pdo' => 
    object(PDO)[2]

所以它似乎被定义为我,并且它说它是一个对象所以我不知道它为什么说它是一个非对象。

以下是该类的方法和实例化:

public function loggedIn(){
    return (isset($_SESSION['user_id']));
}

$Check = new check($db);

参数中的$ db变量是我的pdo对象变量。

现在,当我尝试访问受这些方法保护的页面时,会出现这些错误:

class content{
protected $Check;

public function __construct($Check){
    $this->Check = $Check;
}

public function protectPage(){

    if(!$this->Check->loggedIn()){
        include '/includes/overall/header_main.php';
        echo '<div class="msg_module">';
        echo '<h2>Protected Page:</h2>';
        echo 'The page you are trying to access requires you to login.<br /><br />';
        echo '<a href="index.php">Login</a>';
        echo '</div>';
        include '/includes/overall/footer_main.php';
        exit();
    }
}

public function loggedRedirect(){
    if($this->Check->loggedIn()){
        include '/includes/overall/header_main.php';
        echo '<div class="msg_module">';
        echo '<h2>Protected Page:</h2>';
        echo 'Sorry, you cannot access that page right now.<br /><br />';
        echo '<a href="index.php">Login</a>';
        echo '</div>';
        include '/includes/overall/footer_main.php';
        exit();
    }
}
}

$Content = new content($Check);

我称之为:

$Content->loggedRedirect();
$Content->protectPage();

这让我难以忍受了好几天,我感谢所有的帮助!我希望我已经包含了所需的所有信息。

编辑:这是$ Check变量的正确var_dump

object(check)[3]
 protected 'pdo' => 
  object(PDO)[2]

0 个答案:

没有答案