0

How would I run a function on an array change? For example, whenever I read, write, or whatever with an array ($_SESSION) I want to run a function (which will set a variable inside the session array, but I do not want this to run the function as well).

I need to do this because I plan to make a timestamp that updates whenever the array $_SESSION is used. Is this possible?

For example:

session_start();
$_SESSION["foo"] = "bar"; //run the function!
$foo = $_SESSION["foo"]; //run the function! (optional, though preferred)

The function would be something like this:

$_SESSION["timestamp"] = time();  //do not run the function!

Thanks for any suggestions!

4
  • 3
    Changes to arrays don't fire events. You can create a function or class to wrap/set/get the values you're wanting to set/get, and call that and run your other function/method. PHP classes also have the __set() and __get() magic methods, which can be used when setting or getting properties. Commented Jul 20, 2014 at 1:57
  • @JaredFarrish Thanks! Do you have any idea how to add __get() to the $_SESSION variable, or am I just being plain stupid now? Commented Jul 20, 2014 at 2:01
  • You would need to use a class. See: php.net/manual/en/language.oop5.overloading.php Commented Jul 20, 2014 at 2:03
  • Basic example: codepad.org/98p3uakc Commented Jul 20, 2014 at 2:13

2 Answers 2

2

You can use a basic class to wrap around the $_SESSION. Below is a simple example using the __set() and __get() magic methods to intercept setting your values.

Note that I've used a class property $this->_SESSION because codepad doesn't allow me to use a real session.

<?php

class SessionSetting {

    private $_SESSION = array();

    function __set($name, $value) {
        $this->_SESSION[$name] = $value;

        $this->value_saved($name, $value);
    }

    function __get($name) {
        $this->value_accessed($name);

        return $this->_SESSION[$name];
    }

    function value_saved($name, $value) {
        echo "\$_SESSION['$name'] set to '$value'\n";
    }

    function value_accessed($name) {
        echo "\$_SESSION['$name'] accessed ({$this->_SESSION[$name]})\n";
    }

}

$ss = new SessionSetting;

$ss->prop1 = 'test';
$ss->prop2 = 'test';

echo "$ss->prop1\n";
echo "$ss->prop2\n";

?>

http://codepad.org/98p3uakc

Produces:

$_SESSION['prop1'] set to 'test'
$_SESSION['prop2'] set to 'test'
$_SESSION['prop1'] accessed (test)
test
$_SESSION['prop2'] accessed (test)
test
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply. I will try to implement this into my software.
1

Not exactly sure what you're trying to do, but it's possible with ArrayAccess

class ArrayThing implements \ArrayAccess{
  protected $data = array();

  public function offsetExists($key){
    return array_key_exists($this->data, $key);
  }   

  // $foo = $_SESSION["foo"];
  public function offsetGet($key){
    // run your function    
    return $this->data[$key];
  }

  // $_SESSION["foo"] = "bar";
  public function offsetSet($key, $value){
    // if($key != 'timestamp')
    //   run your function ?
    $this->data[$key] = $value;
  }  

  public function offsetUnset($key){
    unset($this->data[$key]);  
  }    

}

$_SESSION = new ArrayThing();

I should mention that you'll probably need to convert the object into an array when you're done (and also import the session contents in the constructor) if you want it to act like the session superglobal.

1 Comment

Thanks for your quick reply! I will defiantly look into ArrayAccess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.