parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' error in activecollab model class

12,659

Solution 1

    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

  ...
  ..

  }

Solution 2

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.

Solution 3

<?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

Share:
12,659
Shadman
Author by

Shadman

An experienced Software Engineer/Architect, Techie, Entrepreneur, Traveller. Having worked within the industry for over 10.5 years. crazy about softwares &amp; startups

Updated on June 05, 2022

Comments

  • Shadman
    Shadman almost 2 years

    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();
    
    • Michael Berkowski
      Michael Berkowski almost 12 years
      I see you're using $this inside the static function canAccess(), which is not permissible, but shouldn't cause that particular error
    • Mike B
      Mike B almost 12 years
      Perfect example of why curly-braces should never be omitted for control structures. You're not being clever or tidy by leaving them out. Voted to close. Or maybe a dupe?
    • NibblyPig
      NibblyPig almost 12 years
      I disagree, it's a better example of why you should have curly braces on the next line, instead of at the end of a line.
    • Mike B
      Mike B almost 12 years
      @SLC Where do see a trailing closing curly brace in his code? All of his } are on their own line or silently-implied (which was the focus of my point)
    • NibblyPig
      NibblyPig almost 12 years
      @Mike if you see his foreach(...) { has the { on the same line. If he put it onto a newline it would be more obvious there was a missing } because they wouldn't line up vertically.
    • Mike B
      Mike B almost 12 years
      @SLC To each his own. The last one is the most-obvious to me b/c the last line isn't indented back to the gutter like it should be. It's worse (imho) in the first two because of the explicit indent of the braceless if condition.
  • Styxxy
    Styxxy almost 12 years
    That foreach is kind of useless since it now only loops once.
  • Styxxy
    Styxxy almost 12 years
    Not looking further than the stated is not always the best approach to answer and help a person.
  • psx
    psx almost 12 years
    Does it resolve the error as asked by the OP? :) I think yes.
  • Styxxy
    Styxxy almost 12 years
    As a matter of fact, no it doesn't. It introduces other errors (like that "only loop once in foreach").
  • psx
    psx almost 12 years
    Nope. It doesn't introduce that at all. How does my fix (of one closing brace) introduce a bug where the foreach only goes round once? That would -always- happen given this code. It's not introduced, it was in the original code. Granted, the original code has faaaar more errors than just a missing bracket, but that bracket was causing the given error in the original post. Therefore that is what I answered. End of story.
  • Ilija
    Ilija almost 12 years
    public and private are not required. When omitted from method declaration, public will be used. Marking methods as static on the other hand is encouraged, because PHP raises warning (or notice, can't remember) when method that's not marked as static is called statically.
  • Styxxy
    Styxxy almost 12 years
    True it is not required, but it is in my opinion good practice to do so.
  • Shadman
    Shadman almost 12 years
    @Styxxy yes no need to add else condition .. but the thing is that this value if($role->getPermissionValue($name)) is present in foreach everytime .. so I just want to check its position if it is on first then it is true else must return false .. so that's why i need it .. and I can do this without foreach too by using array with first index.