Re: [PHP] is there a static constructor?
Why not just create a no named function in the top of the class. This this way it loads on include. Richard Buskirk Sent from my iPhone On Mar 31, 2011, at 12:56 AM, "D. Dante Lorenso" wrote: > All, > > I want to build a config file class that gets called statically. Is there > such a thing as a static constructor? Example: > > class Daz_Config { > public static function load() { >... > } > public static function get($key) { >self :: load(); >... > } > } > > Daz_Config :: get('myvalue'); > > I want to call the load function when the class is used for the first time. > If no code ever calls "Daz_Config :: get(...)" then I never want to invoke > load() but if it does get called, I only want to call load() once before the > class is used further. > > Anyone know how to do this with calling load() at the top of all the other > functions and writing a load() function that exits early if already loaded? > > -- Dante > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] is there a static constructor?
class Daz_Config { private static $instance; private function __construct(){ $this->load(); } private function load() { ... } public function get($key) { ... } public static getInstance(){ if (!self::$instance) self::$instance = new Daz_Config(); return self::$instance; } } Then you can do: Daz_Config::getInstance()->get('myvalue');
Re: [PHP] is there a static constructor?
On 31 March 2011 06:56, D. Dante Lorenso wrote: > All, > > I want to build a config file class that gets called statically. Is there > such a thing as a static constructor? Example: > > class Daz_Config { > public static function load() { > ... > } > public static function get($key) { > self :: load(); > ... > } > } > > Daz_Config :: get('myvalue'); > > I want to call the load function when the class is used for the first time. > If no code ever calls "Daz_Config :: get(...)" then I never want to invoke > load() but if it does get called, I only want to call load() once before the > class is used further. > > Anyone know how to do this with calling load() at the top of all the other > functions and writing a load() function that exits early if already loaded? > The concept doesn't really make sense - a class that never gets instantiated never gets constructed, hence no static constructor (nor a static destructor). You'll have to call your "constructor" function at the top of the static methods you'll be using - just check inside the constructor if it's been called before or not. Regards Peter -- WWW: plphp.dk / plind.dk LinkedIn: plind BeWelcome/Couchsurfing: Fake51 Twitter: kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] is there a static constructor?
There is such thing, but its not called static constructor, its called singleton pattern class SingletonA { public static instance; public SingletonA(){} public static function getInstancce() { if(self::instance != null) { return self::instanc; } return new SingletonA(); } } On 3/31/2011 12:56 AM, D. Dante Lorenso wrote: All, I want to build a config file class that gets called statically. Is there such a thing as a static constructor? Example: class Daz_Config { public static function load() { ... } public static function get($key) { self :: load(); ... } } Daz_Config :: get('myvalue'); I want to call the load function when the class is used for the first time. If no code ever calls "Daz_Config :: get(...)" then I never want to invoke load() but if it does get called, I only want to call load() once before the class is used further. Anyone know how to do this with calling load() at the top of all the other functions and writing a load() function that exits early if already loaded? -- Dante -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php