0

I have 2 classes, one named site_navigation and the other named member_config.

I have created a function in my site_navigation called getNavBar.

Inside this function, I try to get my users level from a function named userLevelValidation inside a different class called member_config.

This is my class for 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();

And this is my class for my 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();

How would I go about calling the userLevelValidation(); inside my site_navigation class, inside the getNavBar function ?

I have tried putting this : $usersLevel = $member_config->userLevelValidation(); but it says

Fatal Error: Call to a member function userLevelValidation() on null.
12
  • 1
    php.net/manual/en/language.variables.scope.php and you should look into dependency injection as well as you really should not use global for your database connection. Commented Jul 10, 2017 at 12:07
  • you've not initialized the class in site_navigation class or you can inherit the class. like making class site_navigation extends member_config { you must include the both class in main file or have both classes in main file. Commented Jul 10, 2017 at 12:09
  • Classes are at same file? Commented Jul 10, 2017 at 12:09
  • if the function needs what's in the othe class instance, you could pass the instace as parameter. It it is independant from instances, you can make it a static function Commented Jul 10, 2017 at 12:12
  • @GulmuhammadAkbari yes the classes are in the same file, underneath eachother. Commented Jul 10, 2017 at 12:18

1 Answer 1

1

Do as follow:

<?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();
?>
Sign up to request clarification or add additional context in comments.

1 Comment

brilliant! Works perfectly in this format. I just needed to re add in $member_conf = new member_config();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.