在函数中返回值的正确方法,然后检查函数的值

时间:2015-12-16 01:05:22

标签: php

我试图找出在函数内返回值的正确方法。 稍后在我的代码中,我需要检查此函数的值(true或false)并执行操作,具体取决于它是真还是假。

以下是我尝试设置的功能(您会注意到我的主要功能中有一个功能,我不知道php是否允许这样做?):

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;

    }

scanDirectory function工作正常,但我不确定$getprivileges是否会返回checkPrivileges function(它应该是真还是假),这是正确的方法吗?

稍后在我的代码中,主页面通过include显示,在此页面中我必须检查checkPrivileges是真还是假,并显示相应的内容。

以下是我目前在页面上查看checkPrivileges的方式:

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

毋庸置疑,这不起作用。脚本停止执行。

我正在寻找一些帮助以使我的功能发挥作用,如果可能的话,对我不了解的内容有一些解释,我将来会经常使用这种东西

感谢您的帮助!

(16/12/2015 15h50)更新:

根据您的建议,这就是我现在所拥有的:

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);
    }

因此,如果我完全理解我做了什么,checkPrivileges method将返回true,如果((gator::checkPermissions('rw')) || ($userdir == $directory && gator::checkPermissions('rp')))如果不是(并且这是我遇到问题),则应该调用scanDirectory函数。

看起来scanDirectory函数没有返回任何值,我的页面将停止在这行代码中执行:

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

更具体地说,看起来我对scanDirectory函数或者checkPrivileges的这一部分所做的修改有问题:

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

再次感谢所有帮助

1 个答案:

答案 0 :(得分:1)

我不会在类方法(你所谓的主函数)中创建一个函数。你应该把它变成你班级中的私人功能,你可以使用$ this-&gt; FUNCTIONNAME();如图所示:

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;
  }
}

我评论过一些我认为你下次应该考虑的事情