Re: class variable shared by all objects

From: Date: Wed, 16 Oct 2002 20:31:09 +0000
Subject: Re: class variable shared by all objects
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-120235@lists.php.net to get a copy of this message
christophe barbe wrote: > > I guess the answer is obvious but can't find it in the manual. > > Is it possible to have a variable in a class that is shared by all > instances. I thought 'static' would do it but apparently not. I don't recall such a thing, but if you can't find an option for a class variable, implement a method which emulates a class variable system. function getSetClassVar( $name, $set=null ) { static $values = array(); if( !is_null( $set ) ) { $values[$name] = $set; } return isset( $values[$name] ) ? $values[$name] : null; } function getClassVar( $name ) { return $this->getSetClassVar( $name ); } function setClassVar( $name, $value ) { return $this->getSetClassVar( $name ); } HTH, Rob. -- .-----------------. | Robert Cummings | :-----------------`----------------------------. | Webdeployer - Chief PHP and Java Programmer | :----------------------------------------------: | Mail : mailto:robert.cummings@webmotion.com | | Phone : (613) 731-4046 x.109 | :----------------------------------------------: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `----------------------------------------------'

« previous php.general (#120235) next »