hvle Posted November 7, 2006 Share Posted November 7, 2006 Why this will not work in php 5?static $myVar = new MyClass();this gave mesyntax error, unexpected T_NEWAm I doing something wrong or is there a good reason why this won't work? Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/ Share on other sites More sharing options...
bqallover Posted November 7, 2006 Share Posted November 7, 2006 You need a visibility operator in front of 'static', like 'public' or 'private'. Also are you declaring that variable outside a class? As far as I know, static variables are restricted to classes, though I miay be wrong.Check out [url=http://uk.php.net/manual/en/language.oop5.static.php]static keyword[/url] at PHP.net Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120918 Share on other sites More sharing options...
hvle Posted November 7, 2006 Author Share Posted November 7, 2006 actually, I tried to create a singleton.private static singleInstance = new MyClass(); is what I have inside MyClass class Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120920 Share on other sites More sharing options...
bqallover Posted November 7, 2006 Share Posted November 7, 2006 Isn't that fatally recursive?If you're just looking for a static class that you don't have to instantiate, declare the members of a standard class 'static' and then access them with the :: operator, like this (from the above linked page)...[code]class Foo{ public static $my_static = 'foo'; . . .}print Foo::$my_static . "\n";[/code]Is that what you're after? Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120927 Share on other sites More sharing options...
redbullmarky Posted November 7, 2006 Share Posted November 7, 2006 i maybe wrong, but surely it'd be better as:[code]<?phpclass MyClass{ private static $my_instance; function getInstance() { $this->my_instance =& new MyClass(); }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120930 Share on other sites More sharing options...
Jenk Posted November 7, 2006 Share Posted November 7, 2006 it would have to be:[code]<?phpclass MyClass{ private static $my_instance; function getInstance() { self::$my_instance = new MyClass(); }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120931 Share on other sites More sharing options...
redbullmarky Posted November 7, 2006 Share Posted November 7, 2006 [quote author=Jenk link=topic=114130.msg464240#msg464240 date=1162908769]it would have to be:[code]<?phpclass MyClass{ private static $my_instance; function getInstance() { self::$my_instance = new MyClass(); }}?>[/code][/quote]whoops my bad. cheers ;) Link to comment https://forums.phpfreaks.com/topic/26442-oo-with-php-5-question/#findComment-120934 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.