tags:

views:

56

answers:

5

I have a set of defined constants, and for some I'm serializing new instances of a class and setting as a constant (probably not the best idea, I know...), the problem is that when information changes in the instance of the class, it doesn't update the referenced memory that was declared in the constant... I know a constant cannot be altered, but isn't it really just pointing to a block of memory and when that memory information is changed, so should the returning value of the constant? Well before I confuse anyone with what I said, I'll just show you what I have done:

<?php
    class Collection {
        var $items = array();

        public function __construct() { }

        public function add($val) {
            array_push($this->items, $val);
        }

        public function dump() {
            var_dump($this->items);
        }
    }

    $collection = new Collection();
    define('COLLECTION', serialize(&$collection));
    unserialize(COLLECTION)->add('test item 1');
    unserialize(COLLECTION)->add('test item 2');
    unserialize(COLLECTION)->dump();
    /* It will end up dumping this:
       array(0) { }
     */
?>

Now the reason why I'm doing this is because I'll be using these constants in many(30+) different php files, inside many functions, I know this can be accomplished by simple just using the variable and using global $variable; in every function, but I'm trying to avoid it since most servers have globals turned off. Please tell me if there is a better way to approach it aswell.

- Thanks!
  Nadeem

+2  A: 

Why not just define a function?

function push_item($item) {
  global $collection;
  $collection->push($item);
}

It's a lot cleaner than defining a constant of a serialized class (that's almost WTF territory, sorry).

cletus
+4  A: 

A better option would be to implement the Singleton pattern or something like this, to avoid serialization:

class Collection {
    var $items = array();

    public function __construct() { }

    public function add($val) {
        array_push($this->items, $val);
    }

    public function dump() {
        var_dump($this->items);
    }

    public function push($v) {
        $this->items[]= $v;
    }

    static protected $_instance = null;
    static public function get() {
       if (empty(self::$_instance)) self::$_instance= new Collection();
       return self::$_instance;
    }

}

Collection::get()->push('test item 1');
Collection::get()->push('test item 2');
Collection::get()->dump();
pygorex1
A: 
Nadeem
To alert future posters, edit you question and place that 'solved' text there.
AntonioCS
+1  A: 

Your constant is storing a string. When you unserialise it, the value of the constant doesn't change no matter what you do to the class you created from it (the clue's in the name really...). Take Brad's advice in the comment on your question and use a registry.

Greg
+1  A: 

... since most servers have globals turned off.

There are several points of confusion in this question, and this is one of them. Most servers have the option register_globals turned off. That doesn't prevent you from using global variables in your application. All that the option does is that it imports values from $_GET and $_POST (and a few more) as global variables from the beginning of your script.

troelskn