-2

i am saving a value to be called later by other classes because I need this value and can't save it to a cookie because its needs a page refresh and can't make the value global for some reason. It sets the value but will not return it and can't work out why.

class security{
static function auth_key($request, $data=false){


    if($request=="set"){
        //this is always set first and works.
        $auth_key_value=$data;  
    }
    if($request=="get"){
       //If i try die("test"); here is does not die, so its like it does not call it.
        return $auth_key_value;
    }       
}

}

and the call

echo $key = security::auth_key("get");

This returns nothing, what am I doing wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rob
  • 302
  • 2
  • 3
  • 15
  • 8
    uh, die'ing? remove the die! – Farkie May 30 '13 at 23:04
  • I have now, just used that to see if it was being called and its not :\ – Rob May 30 '13 at 23:05
  • it's not a static class. Do you want to call it statically or normally? – Farkie May 30 '13 at 23:06
  • [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) - enable error reporting to the highest possible level and follow the error log. also enable display of errors in your development environment. http://stackoverflow.com/a/14504459/367456 – hakre May 30 '13 at 23:06
  • 1
    The variable `$auth_key_value` isn't defined? Turn on E_NOTICE... – bwoebi May 30 '13 at 23:06
  • use static instead public function if you want to call it this way – bksi May 30 '13 at 23:06
  • I have tried public and static, both do not work – Rob May 30 '13 at 23:06
  • Can we assume that you are setting it first? – Farkie May 30 '13 at 23:08
  • first of all security::auth_key("get"); is a static method call and you should add the static keyword to the method definition, second try adding something to $data, at the moment it is false which cant be viewed with echo – lexmihaylov May 30 '13 at 23:08
  • first if you want to use static, you should declare both class and method as static. Otherwhise you just have to init new instance of the class to get access to its methods – bksi May 30 '13 at 23:08
  • Don't you also need to declarer `$auth_key_value` as a static property? – Barmar May 30 '13 at 23:08
  • As Barmar said you should set this property before return it – bksi May 30 '13 at 23:10
  • Sorry should of said, it is set first, always. – Rob May 30 '13 at 23:12
  • but if it's static - it doesn't persist from your set to your get – Farkie May 30 '13 at 23:13

4 Answers4

2

You need to create a class member first, i.e.:

class security{

private static $auth_key_value;

public function auth_key($request, $data=false){
    if($request=="set"){
        self::$auth_key_value=$data;  
    }
    if($request=="get"){
        return self::$auth_key_value;
    }       
}

}

Edit: Changed to static, as noted in the comments below

Filippos Karapetis
  • 4,367
  • 21
  • 39
0

SOLVED, thanks for the help from @Filippos Karapetis

public static $auth_key_value;

static function auth_key($request, $data=false){

    if($request=="set"){
        self::$auth_key_value=$data; 
    }
    if($request=="get"){
        return self::$auth_key_value;
    }       
}

Thanks to everyone who helped :)

Rob
  • 302
  • 2
  • 3
  • 15
0

Where to start: First you should know how to declare and use classes. If you want to use static class, you should declare static methods and properties. Example:

class security {
    private static $auth_key_value;
    public static function auth_key($request, $data=false) {
        if($request=="set"){
            self::auth_key_value=$data;  
        }
        if($request=="get"){
            return self::auth_key_value;
        } 
    }
}

Then after that you should SET your variable with:

security::auth_key("set", "foo");

And now you can use your echo.

The problem here is that i don't suggest using static class for this situation.

My suggestion is to init an instance of the class and use setter and getter:

class security {
    private $authKeyValue;
    public function setAuth($value) {
        $this->authKeyValue = $value;
    }
    public function getAuth() {
        return $this->authKeyValue;
    }
}

and in your code:

$authObject = new security();

$authObject->setAuth("blah blah");

echo $authObject->getAuth();
bksi
  • 1,606
  • 1
  • 23
  • 45
  • First example wouldn't work, $this is not authorised inside a static method. you need to use a static variable. – Ben May 30 '13 at 23:27
0

An Addition to Filippos Karapetis Answer: Further Explanation.

In order to 'Store' something you need 'space' to store it. In your original answer you are storeing it in in $auth_key_value.

Now, the second topic, Scope. In PHP ( and quite a few other languages) local variables are destoryed when you leave the scope. The simplest example of scope is a function.

Putting it together. In your function auth_key, you declare a variable $auth_key_value. You then assign $data to it. When the function returns, this variable is 'destroyed'. Then, when you call the security::auth_key("get"); it recreates the $auth_key_value variable. It has no value on declaration here so your echo returns nothing. This is what it should be doing.

If you notice in Filippos Karapetis Answer: He declares private static $auth_key_value; at the class level. That is outside the function. That means that the $auth_key_value lives in the class and will be 'destroyed' when the class is destroyed, not the function.

You should review scope, it would be very helpful for you.

SH-
  • 1,642
  • 10
  • 13