Question

I'm working on activecollab custom module's permissions, and getting this error message when try to calling function of static method dont know why; please do help will be really appericiatable ..

Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in D:\wamp\www\activecollab\public\activecollab\3.0.9\modules\projectcomrequest\models\Projectcomrequests.class.php on line 130

the code I did in model file is:

  class Projectrequests extends DataManager {

   ...
   ....

        function getPermissionValue($name){
            $roles = Roles::find();
            foreach($roles as $role) {
                if($role->getPermissionValue($name))
                    return true;
                else
                    return false;
        }

        static function canAccess() {
          if(self::getPermissionValue('can_use_project_request')) return true;
            return false;
        } // canAccess

  ...
  ..

  }

calling in controller by this:

echo Projectrequests::canAccess();
Was it helpful?

Solution

    foreach($roles as $role) {
        if($role->getPermissionValue($name))
            return true;
        else
            return false;

You're missing a closing } there. So it should be:

  class Projectrequests extends DataManager {

   ...
   ....

        function getPermissionValue($name){
            $roles = Roles::find();
            foreach($roles as $role) {
                if($role->getPermissionValue($name))
                    return true;
                else
                    return false;
            } // <-- here
        }

        static function canAccess() {
          if(self::getPermissionValue('can_use_project_request')) return true;
            return false;
        } // canAccess

  ...
  ..

  }

OTHER TIPS

A static method doesn't have a class context $this as you try to call in the first line of canAccess(). You should call self:: instead of $this-> to access the class context and then you can only call other static field and methods. You will have to make getPermissionValue also static.

A few more errors:

  • You forgot a { in your foreach. Fixed this for you (only return true inside the loop, the else construction is useless because otherwise your foreach only loops once).
  • You can immediately return the value of the call to getPermissionValue in canAccess since it is a boolean anyway (the if-else construction is kind of useless).

Corrected code:

static function getPermissionValue($name){
    $roles = Roles::find();
    foreach($roles as $role) {
        if($role->getPermissionValue($name))
            return true;
    }    
    return false;
}

static function canAccess() {
    return self::getPermissionValue('can_use_project_request');
} // canAccess

I would like to advice as well to use access modifiers like public and private as it is good practice.

<?php
class Projectrequests extends DataManager {

   ...
   ....

        function getPermissionValue($name){
            $roles = Roles::find();
            foreach($roles as $role) {
                if($role->getPermissionValue($name))
                    return true;
                else
                    return false;
            } // <!---- YOUR ERROR IS HERE
        }

        static function canAccess() {
          if($this->getPermissionValue('can_use_project_request')) return true;
            return false;
        } // canAccess

  ...
  ..

  }

Also, static methods do not have access to $this you need to use self:: instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top