apatik
apatik

Reputation: 383

correct way to return a value in a function, and then check the function's value

I'm trying to figure out the right way to return a value inside a function. Later in my code I need to check this function's value (true or false) and execute an action depending if it is true or false.

Here is the function I'm trying to set up (you'll notice there is a function inside my main function, I don't know if php allows that ?) :

public function checkPrivileges($getprivileges) {

        $directory = $_SESSION['cwd'];                  // current directory
        $user = $_SESSION['simple_auth']['username'];   // get username
        $repository = gatorconf::get('repository');     // get base repertory of the file manager
        $userdir = $repository.DS.'user'.DS.$user;      // user's repertory

        function scanDirectory($userdir = '', $directory){
            $folders = glob($userdir . '/*' , GLOB_ONLYDIR);
            foreach($folders as $folder){
                if (($folder == $directory && gator::checkPermissions('rp')) || (gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
                    return true;
                }

                $scan_result = scanDirectory($folder, $directory);

                if($scan_result) {
                   return true;
                }
            }
            return false;
        }

        scanDirectory($userdir, $directory);

        if ((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
                $getprivileges = true;
        }

        $getprivileges = scanDirectory($userdir, $directory);   // user's privileges
        return $getprivileges;

    }

The scanDirectory function is working fine, but I am not sure if $getprivileges is returned to the checkPrivileges function (it's supposed to be either true or false), is this the right way to do it ?

Later on my code the main page is displayed through an include, and inside this page I have to check if checkPrivileges is true or false, and display something accordingly to it.

Here is how I currently check checkPrivileges on the page :

<?php
    if (gator::checkPrivileges($getprivileges) == true){
        echo "it's a bingo";
    }else {
        echo "that was close";
    }
?>

Needless to say this is not working. The script stops executing.

I'm looking for some help to get my function to work, and if possible some explanations about what I didn't understand, I'll have to this kind of stuff quite often in the future.

Thanks for the help !

(16/12/2015 15h50) Update :

After your suggestions this is what I have right now :

public function checkPrivileges($getprivileges) {
        $directory = $_SESSION['cwd'];                  // current directory
        $user = $_SESSION['simple_auth']['username'];   // get username
        $repository = gatorconf::get('repository');     // get base repertory of the file manager
        $userdir = $repository.DS.'user'.DS.$user;      // user's repertory


        if ((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
                return true;
        }

        $getprivileges = $this->scanDirectory($userdir, $directory);   // user's privileges
        return $getprivileges;

    }

    private function scanDirectory($userdir = '', $directory) {
        $folders = glob($userdir . '/*' , GLOB_ONLYDIR);
        foreach($folders as $folder){
            if (($folder == $directory && gator::checkPermissions('rp')) || (gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
                return true;
            }

            if($this->scanDirectory($folder, $directory) === true) {
                return true;
            }
        }
        return false;

        scanDirectory($userdir, $directory);
    }

So if I fully understand what I did, the checkPrivileges method will return true if if ((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) and if not (and this is where I have a problem), it is supposed call the scanDirectory function.

It looks like no value gets returned from the scanDirectory function, my page will just stop executing at this line of code :

<?php
    if (gator::checkPrivileges($getprivileges) == true) {
        echo "yata";
    }else {
    echo "nope";
    }
?>

To be more specific it looks like there is something wrong either with the modifications I made to the scanDirectory function, or with this part of checkPrivileges :

$getprivileges = $this->scanDirectory($userdir, $directory);   // user's privileges
        return $getprivileges;

Thanks again for all the help

Upvotes: 0

Views: 68

Answers (1)

Rolf
Rolf

Reputation: 78

I wouldn't make a function inside a class method (your socalled main function). You should make it a private function inside your class, you can than call it using $this->FUNCTIONNAME(); as shown:

Class Privileges {

  public function checkPrivileges($getprivileges) {
    $directory = $_SESSION['cwd'];                  // current directory
    $user = $_SESSION['simple_auth']['username'];   // get username
    $repository = gatorconf::get('repository');     // get base repertory of the file manager
    $userdir = $repository.DS.'user'.DS.$user;      // user's repertory

    // I think you should delete this, because you call it without assing the return data to a variable.
    //$this->scanDirectory($userdir, $directory);

    if ((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
            //$getprivileges = true;
            return true;
    }
    // Why are you setting $getprivileges to true above this comment and 
    // than re-setting it below this comment? If the below line is no longer
    // necessary if the earlier conditions where met you should simply 
    // return true above this comment (as I have done for you now)
    $getprivileges = $this->scanDirectory($userdir, $directory);   // user's privileges
    return $getprivileges;

  }

  private function scanDirectory($userdir = '', $directory) {
        $folders = glob($userdir . '/*' , GLOB_ONLYDIR);
        foreach($folders as $folder){
            if (($folder == $directory && gator::checkPermissions('rp')) || (gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp'))) {
                return true;
            }

            if($this->scanDirectory($folder, $directory) === true) {
               return true;
            }
        }
        return false;
  }
}

I have commented some things I think you should consider next time

Upvotes: 1

Related Questions