PHP - 从另一个类的另一个函数调用一个函数

时间:2017-07-10 12:02:57

标签: php class

我有两个类,一个名为 site_navigation ,另一个名为 member_config

我在site_navigation中创建了一个名为getNavBar的函数。

在这个函数中,我尝试从另一个名为member_config的类中的一个名为userLevelValidation的函数中获取用户级别。

这是我的site_navigation类:

class site_navigation {

function getNavBar() {
global $con;
$usersLevel = $member_config->userLevelValidation();
$page_link = $_SERVER['REQUEST_URI'];

    if($usersLevel == "Administrator") { 
        ?>
            <li class="sidebar-heading"> <b><i>Seller</i></b> Tools</li>
                <li <?php if ($page_link == "members.php") { echo "class='-active'";}?>><a href="members.php"> <i class="fa fa-home"></i> <span>Dashboard</span></a></li>
                <li <?php if ($page_link == "mTokens.php") { echo "class='-active'";}?>><a href="mTokens.php"> <i class="fa fa-tachometer"></i> <span>Redeem / My Tokens</span></a></li>
                <li <?php if ($page_link == "mAccount.php") { echo "class='-active'";}?>><a href="mAccount.php"> <i class="fa fa-user"></i> <span>Manage My Account</span></a></li>
                <li <?php if ($page_link == "mSupport.php") { echo "class='-active'";}?>><a href="mSupport.php"> <i class="fa fa-spinner"></i> <span>Support</span></a></li>
            </li>
            <li class="sidebar-heading"> <b><i>Admin / Staff  </i></b> Tools</li>
                <li <?php if ($page_link == "admin_dash.php") { echo "class='-active'";}?>><a href="admin_dash.php"> <i class="fa fa-dashboard"></i> <span>Dashboard</span></a></li>
                <li <?php if ($page_link == "admin_members.php") { echo "class='-active'";}?>><a href="admin_members.php"> <i class="fa fa-users"></i> <span>Manage Clients</span></a></li>
                <li <?php if ($page_link == "admin_site.php") { echo "class='-active'";}?>><a href="admin_site.php"> <i class="fa fa-cogs"></i> <span>Manage Site</span></a></li>
                <li <?php if ($page_link == "admin_server.php") { echo "class='-active'";}?>><a href="admin_server.php"> <i class="fa fa-server"></i> <span>Manage Server</span></a></li>
                <li <?php if ($page_link == "admin_tokens.php") { echo "class='-active'";}?>><a href="admin_tokens.php"> <i class="fa fa-key"></i> <span>Manage Tokens</span></a></li>
                <li <?php if ($page_link == "admin_updates.php") { echo "class='-active'";}?>><a href="admin_updates.php"> <i class="fa fa-user"></i> <span>Manage Updates</span></a></li>
                <li <?php if ($page_link == "admin_logs.php") { echo "class='-active'";}?>><a href="admin_logs.php"> <i class="fa fa-address-book"></i> <span>Logs</span></a></li>
                <li <?php if ($page_link == "admin_staff.php") { echo "class='-active'";}?>><a href="admin_staff.php"> <i class="fa fa-user-circle-o"></i> <span>Manage Staff</span></a></li>
            </li>
        <?php
    } else if($usersLevel == "Staff") {
            ?>
            <li class="sidebar-heading"> <b><i>User</i></b> Management</li>
                <li <?php if ($page_link == "members.php") { echo "class='-active'";}?>><a href="members.php"> <i class="fa fa-home"></i> <span>Dashboard</span></a></li>
                <li <?php if ($page_link == "mTokens.php") { echo "class='-active'";}?>><a href="mTokens.php"> <i class="fa fa-tachometer"></i> <span>Redeem / My Tokens</span></a></li>
                <li <?php if ($page_link == "mAccount.php") { echo "class='-active'";}?>><a href="mAccount.php"> <i class="fa fa-user"></i> <span>Manage My Account</span></a></li>
                <li <?php if ($page_link == "mSupport.php") { echo "class='-active'";}?>><a href="mSupport.php"> <i class="fa fa-spinner"></i> <span>Support</span></a></li>
            </li>
            <li class="sidebar-heading"> <b><i>Staff</i></b> Tools</li>
                <li <?php if ($page_link == "admin_dash.php") { echo "class='-active'";}?>><a href="admin_dash.php"></i> Dashboard</a></li>
                <li <?php if ($page_link == "admin_members.php") { echo "class='-active'";}?>><a href="admin_members.php"> Manage Clients</a></li>
                <li <?php if ($page_link == "admin_tokens.php") { echo "class='-active'";}?>><a href="admin_tokens.php"> Manage Tokens</a></li>
                <li <?php if ($page_link == "admin_updates.php") { echo "class='-active'";}?>><a href="admin_updates.php"> Manage Updates</a></li>
                <li <?php if ($page_link == "admin_support.php") { echo "class='-active'";}?>><a href="admin_support.php"> Manage Support</a></li>
            </li>
        <?php
    } else if($usersLevel == "Seller") {
            ?>
            <li class="sidebar-heading"> <b><i>Seller</i></b> Tools</li>
                <li <?php if ($page_link == "members.php") { echo "class='-active'";}?>><a href="members.php"> <i class="fa fa-home"></i> <span>Dashboard</span></a></li>
                <li <?php if ($page_link == "mTokens.php") { echo "class='-active'";}?>><a href="mTokens.php"> <i class="fa fa-tachometer"></i> <span>Redeem / My Tokens</span></a></li>
                <li <?php if ($page_link == "mAccount.php") { echo "class='-active'";}?>><a href="mAccount.php"> <i class="fa fa-user"></i> <span>Manage My Account</span></a></li>
                <li <?php if ($page_link == "mSupport.php") { echo "class='-active'";}?>><a href="mSupport.php"> <i class="fa fa-shopping-cart"></i> <span>Support</span></a></li>
            </li>
        <?php
    }
  }
}

//Call the class function for site_config
$site_nav = new site_navigation();

这是我的member_config类:

class member_config {

function grabUserInfos()
{
    global $con;
    $username = $_SESSION["username"];
    $query    = "SELECT id, username, password, email, cpukey, salt, ip, time, titleid, enabled, kvdata, lastonline, profile_picture, userLevel, register_time, customlifetimenotify, gamertag FROM users WHERE username = '$username'";
    $result = $con->query($query) or die("Error");
    return $result->fetch_all();
}

function userLevelValidation()
{
    global $con;
    $users = $this->grabUserInfos();
    foreach ($users as $user) {
        $userLevel = $user[13];
        if ($userLevel == 3) {
            return "Administrator";
        } else if ($userLevel == 2) {
            return "Staff";
        } else if ($userLevel == 1) {
            return "Seller";
        } else
            return "Unknown";
    }
  }
}

//Call the class function for member_config
$member_config = new member_config();

我如何在getNavBar函数中调用site_navigation类中的userLevelValidation();

我试过这个:$usersLevel = $member_config->userLevelValidation();但是它说

Fatal Error: Call to a member function userLevelValidation() on null.

1 个答案:

答案 0 :(得分:1)

执行以下操作:

<?php

class test1
{
   private $name = 'Akbari';

   function getName()
   {
     return $this->name;
   }   
}


class test2
{

     function __construct()
     {
     }

     function callA()
     {
       $test1Object = new test1();
       $name = $test1Object->getName();
       echo $name;    //Prints Akbari
     }
}

//create an object
$test2Object = new test2();
$test2Object->callA();
?>