PHP类问题/会话问题

时间:2014-06-27 13:55:46

标签: php class oop

我正在尝试创建一个对象并将其存储在会话变量中,以便我可以在不同的页面上访问它。

我遇到的问题是,我的对象中的属性无法解释。具体来说,getMenu方法似乎显示了错误的属性。无法弄清楚为什么,也许这里的人一眼就知道了。

这是我的user.class.php:

    <?php

/**
 * Created by PhpStorm.
 * User:
 * Date: 6/26/14
 * Time: 2:55 PM
 */

class user{
    private $company;
    private $userName;
    private $menuType;
    private $viewState;
    private $gridType;

    public function setGrid($gridType){
        $this->gridType = $gridType;

    }
    public function getGrid(){
        if($this->gridType='salesGrid'){
             include 'gridView.php';
        }elseif($this->gridType='truckingGrid'){
             include 'lActiveLoadView.php';
        }
    }

    public function setCompany($company){
        $this->company = $company;

    }
    public function getCompany(){
        return $this->company;
    }

    public function setUserName($userName){
        $this->userName = $userName;

    }
    public function getUserName(){
        return $this->userName;
    }

    public function setMenu($menuType){
        $this->menuType = $menuType;
    }

    public function getMenu(){
        if($this->menuType='salesMenu'){
            $x = <<< 'Menu'
     <button id='carrierEditor'  class='alignRight' onclick="location.href='gridManager.php?action=alert'">Alerts</button>
     <button id='carrierEditor'  class='alignRight' onclick="location.href='gridManager.php?action=carrier'">Carriers</button>
     <button id='vendorEditor'  class='alignRight' onclick="location.href='gridManager.php?action=vendor'">Vendors</button>
     <button id='productEditor'  class='alignRight' onclick="location.href='gridManager.php?action=product'">Products</button>
     <button id='customerEditor'  class='alignRight' onclick="location.href='gridManager.php?action=customer'">Customer</button>
     <button id='home'  class='alignRight' onclick="location.href='index.php'">Home</button>
Menu;

            echo $x;

        }
        elseif($this->menuType='truckingMenu'){
            $x = <<< 'Menu'
            <button id='generateNewLoad' onclick=\"location.href='index.php?viewState=NewLoad'\">Generate New Load</button>
            <button id='openloads'  onclick=\"location.href='index.php?viewState=All'\">Open Loads</button>
            <button id='viewToday'  class='alignLeft' onclick=\"location.href='index.php?viewState=Today'\">Today</button>
            <button id='viewTomorrow'  class='alignLeft' onclick=\"location.href='index.php?viewState=Tomorrow'\">Tomorrow</button>
            <button id='viewWeek'  class='alignLeft 'onclick=\"location.href='index.php?viewState=Week'\">Week</button>
            <button id='trackingloads'  onclick=\"location.href='index.php?viewState=Tracking'\">Tracking Loads</button>
            <button id='closedloads'  onclick=\"location.href='index.php?viewState=Completed'\">Closed Loads</button>
            <button id='deliveryRequest'  class='alignRight'>Delivery Request</button>
            <button id='bolGenerator'  class='alignRight'>Manage BOLs</button>
            <button id='manageContacts'  class='alignRight' onclick=\"location.href='gridManager.php?action=contacts'\">Manage Contacts</button>
            <button id='btnEmailBlast'  class='alignRight' >Email Blast</button>
Menu;
             echo $x;
        }
    }
    public function setViewState($viewState){
        $this->viewState = $viewState;
    }
    public function getViewState(){
        return $this->$viewState;
    }
}

我的身份验证页面:

if ($adldap->authenticate($user, $pass)){
            session_start();
            $_SESSION['loggedInUser'] = $user;

            //Build an array after calling in the inGroup method in /src/adLDAP. In this array we store the KEY as the group name and the VALUE as a boolean value.
            $loginType = array("Administration" => $adldap->user()->inGroup($user, "NY-IT"),
                "Sales" => $adldap->user()->inGroup($user, "ACL-SALES"),
                "Trucking" => $adldap->user()->inGroup($user, "ACL-SALES_ABRIDGED"),
                "PHSales" => $adldap->user()->inGroup($user, "ACL-PH-SALES"),
                "HBroccoli" => $adldap->user()->inGroup($user, "ACL-SALES_BROCCOLI"));
            //Loop through array, if a TRUE value comes up, assign it to the session and redirect to the correct page

            //$userProps = new user();

            //   $company = $_SESSION['userObject']->getCompany();


          //  if (!$_SESSION['userObject'] instanceof User)
          //      $_SESSION['userObject'] = new user();
            $userSession = new user();

            foreach ($loginType as $val) {
                if ($loginType['Administration'] == true) {
                    $userSession->setCompany('Administrators');
                    $userSession->setMenu('salesMenu');
                    $userSession->setUserName($user);
                    $userSession->setGrid('salesGrid');
                    $userSession->setViewState('All');

                    $_SESSION['userObject']=$userSession;

                    $_SESSION['company'] = "Administrators";


                    header('Location: index.php?alerts=null');
                } elseif ($loginType['Sales'] == true) {

                    $userSession->setCompany('Sales');
                    $userSession->setMenu('salesMenu');
                    $userSession->setUserName($user);
                    $userSession->setViewState('All');
                    $userSession->setGrid('salesGrid');

                    $_SESSION['userObject']=$userSession;

                    $_SESSION['company'] = "Sales";
                    header('Location: index.php?alerts=null');

                } elseif ($loginType['Trucking'] == true) {

                    $userSession->setCompany('Trucking');
                    $userSession->setMenu('truckingMenu');
                    $userSession->setUserName($user);
                    $userSession->setViewState('All');
                    $userSession->setGrid('truckingGrid');

                    $_SESSION['userObject']=$userSession;


                    $_SESSION['company'] = "Trucking";
                    header('Location: index.php?alerts=null');
                } elseif ($loginType['PHSales'] == true) {
                    $_SESSION['company'] = "PH Sales";
                    header('Location: cManager.php');
                } elseif ($loginType['HBroccoli'] == true) {
                    $_SESSION['company'] = "HBroccoli";
                    header('Location: index.php?alerts=null');
                } else {
                    header('Location: login.php?alerts=na');
                }
            }
        }
        else{
        header('Location: login.php?alerts=na'); // non authenticated used
        }

然后是一些案例: 加载对象的var转储将公开:

    object(user)[1]
  private 'company' => string 'Trucking' (length=14)
  private 'userName' => string 'gpigb' (length=5)
  private 'menuType' => string 'truckingMenu' (length=12)
  private 'viewState' => string 'All' (length=3)
  private 'gridType' => string 'truckingGrid' (length=12)

如果我调用存储在会话变量中的类中的任何方法,则由于某种原因,方法的属性会被错误的数据写入。

$x = $_SESSION['userObject'];
    $x->getMenu();

对象的vardump显示:

object(user)[1]
  private 'company' => string 'Trucking' (length=14)
  private 'userName' => string 'gpigb' (length=5)
  private 'menuType' => string 'salesMenu' (length=9)
  private 'viewState' => string 'All' (length=3)
  private 'gridType' => string 'truckingGrid' (length=12)

查看菜单类型现在如何显示'salesMenu'?我从来没有调用setMenu方法来改变它,但它有。是什么给了什么?

请帮助

2 个答案:

答案 0 :(得分:1)

user.class.php文件中,您要为=类/对象变量分配(menuType),而不是将其与==进行比较。

if($this->menuType='salesMenu'){
    ...
}
elseif($this->menuType='truckingMenu'){
    ....

应该是:

if($this->menuType == 'salesMenu'){
    ...
}
elseif($this->menuType == 'truckingMenu'){
    ....

您还在覆盖gridType函数中的getGrid()变量。

答案 1 :(得分:0)

您的if条件错误!

正确:

//..

if($this->menuType=='salesMenu'){

//..

elseif($this->menuType=='truckingMenu'){