4

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();
Shadman
  • 755
  • 2
  • 11
  • 19
  • I see you're using `$this` inside the static function `canAccess()`, which is not permissible, but shouldn't cause that particular error – Michael Berkowski May 22 '12 at 12:52
  • 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](http://meta.stackexchange.com/questions/87149/should-syntax-error-questions-be-closed-as-too-localized-after-being-answered). [Or maybe a dupe?](http://stackoverflow.com/search?q=Parse+error%3A+parse+error%2C+expecting+%60T_PAAMAYIM_NEKUDOTAYIM%27) – Mike B May 22 '12 at 12:54
  • 1
    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. – NibblyPig May 22 '12 at 13:07
  • @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) – Mike B May 22 '12 at 14:17
  • @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. – NibblyPig May 22 '12 at 14:48
  • @SLC [To each his own](http://i.imgur.com/xs8kz.png). 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. – Mike B May 22 '12 at 15:41

3 Answers3

5
    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

  ...
  ..

  }
psx
  • 4,040
  • 6
  • 30
  • 59
  • That foreach is kind of useless since it now only loops once. – Styxxy May 22 '12 at 12:59
  • Not looking further than the stated is not always the best approach to answer and help a person. – Styxxy May 22 '12 at 13:02
  • 1
    Does it resolve the error as asked by the OP? :) I think yes. – psx May 22 '12 at 13:02
  • As a matter of fact, no it doesn't. It introduces other errors (like that "only loop once in foreach"). – Styxxy May 22 '12 at 13:39
  • 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. – psx May 22 '12 at 14:38
  • @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. – Shadman May 23 '12 at 06:19
3

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.

Styxxy
  • 7,462
  • 3
  • 40
  • 45
  • 1
    `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. – Ilija May 22 '12 at 16:55
  • True it is not required, but it is in my opinion good practice to do so. – Styxxy May 22 '12 at 18:57
2
<?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

Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54