在方法中返回函数值的正确方法

时间:2015-12-16 20:00:26

标签: php

我有一些麻烦让方法正常工作,现在我有一个确定用户权限的脚本,然后显示页面(main.php),并在main.php内部检查权限和显示关于用户是否具有必要特权的不同之处。

以下是我的functions.php的压缩版本以及相关的方法/函数:

<?php

class gator {

    public function init() {

        // get user's privileges
        $this->checkPrivileges($getprivileges);

        include("header.php");
        include("main.php");
        include("footer.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


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

}

?>

这是我用来检查main.php页面内用户权限的代码的例子:

<?php
    if (gator::checkPrivileges($getprivileges) == true) {
        echo "privileges granted";
    }
    else {
        echo "you don't have the privileges";
    }
?>

我的问题是我的checkPrivileges()方法仅在满足第一个条件且返回true时才有效。如果它不是它似乎没有返回任何东西,并且当我使用if (gator::checkPrivileges($getprivileges) == true)时,我的脚本停止在main.php页面上的某个点上执行(我不完全确定,因为我没有得到任何错误信息)。

无论如何,我的方法确实看起来有些不对劲,我在php方面不是很有经验但是在测试了几件事之后到目前为止看起来它可能来自:

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

也许它无法调用scanDirectory,或者无法返回值,或者别的什么,我完全被卡住了:/

如果有人对此有解决方案或有关我的代码的建议,我将非常感激。

谢谢!

更新:

我使用了error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', '1');,这里是结果(2个错误):

  

严格的标准:非静态方法gator :: checkPrivileges()应该   不能在第34行的main.php中静态调用

if (gator::checkPrivileges($getprivileges) == true) {

  

致命错误:在不在对象上下文中时使用$ this ...   第97行的functions.php

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

这两行都包含在原帖中。

解决方案:

我将两个函数都更改为静态,并修改了我在checkPrivileges方法中调用scanDirectory函数的方式。

public static function checkPrivileges($getprivileges = false) {

    $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 ($userdir == $directory && gator::checkPermissions('rp')) {
        return true;
    }

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

}

private static 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(gator::scanDirectory($folder, $directory) === true) {
            return true;
        }
    }
    return false;

}

0 个答案:

没有答案