NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
[resolved] error when I try to encode a static function


Joined: 20 Nov 2010
Posts: 2
Reply with quote
I get an error if this code is in a file.

Encoder Parse error: syntax error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' at ...

why??
Code:

static function &getStaticProperty($class, $var)
    {
        static $properties;
        if (!isset($properties[$class])) {
            $properties[$class] = array();
        }

        if (!array_key_exists($var, $properties[$class])) {
            $properties[$class][$var] = null;
        }

        return $properties[$class][$var];
    }
View user's profileFind all posts by itctrlSend private message


Joined: 11 Apr 2010
Posts: 90
Location: Prague, Czech Republic
Reply with quote
Hello.
First, the ...static function... has to be inside a class, otherwise you got a syntax error. Second you do not have $properties initialized. And finally it is far better to put the ...static $properties... as a static property of the class. Assuming my guess for your code functionality is correct, it should look like this if you want it as a function:
Code:

function &getStaticProperty($class, $var) {

    static $properties = array();

    if (!isset($properties[$class])) {
        $properties[$class] = array();
    }
    if (!array_key_exists($var, $properties[$class])) {
        $properties[$class][$var] = null;
    }
    return $properties[$class][$var];
}

or like this if you want it as a class:
Code:

class YourClassName {

    static protected $properties = array();

    static public function &getStaticProperty($class, $var) {
        if (!isset(self::$properties[$class])) {
            self::$properties[$class] = array();
        }
        if (!array_key_exists($var, self::$properties[$class])) {
            self::$properties[$class][$var] = null;
        }
        return self::$properties[$class][$var];
    }
}
View user's profileFind all posts by fibizaSend private message
[resolved] error when I try to encode a static function
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic