note 27512 deleted from language.oop by didou

From: Date: Sun, 07 Dec 2003 02:40:45 +0000
Subject: note 27512 deleted from language.oop by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-61548@lists.php.net to get a copy of this message
Note Submitter: duncan@NOTINNED.MEAT.emarketeers.com ---- If you want to use class variables you should use a static method, not a global as suggested above. class Counter { function Counter($name) { $this->name = $name; $this->id = Counter::id(); } function &_classObj() { static $classObj; if(!is_object($classObj)) { $classObj = new stdClass(); $classObj->id=0; } return $classObj; } function id() { $co =& Counter::_classObj(); $co->id++; return $co->id; } function sayHello() { echo "Hello, I am $this->name and my ID is $this->id\n"; } } $a = new Counter("Bill"); $b = new Counter("Ben"); $a->sayHello(); $b->sayHello(); **** Prints **** Hello, I am Bill and my ID is 1 Hello, I am Ben and my ID is 2

« previous php.notes (#61548) next »