EricIsGood Posted December 11, 2008 Share Posted December 11, 2008 <?php Class users { public static $usersToCacheByID = array(); } users::$usersToCacheByID[] = 'User1'; users::$usersToCacheByID[] = 'User2'; echo '<pre>usersToCacheByID ' . users::$usersToCacheByID . '</pre>'; ?> I expect this to print out 'User1' and 'User2' when I print the array. Thanks for your help, -Eric Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/ Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 You would need to create an instance of the users class first: $u = new users(); u::$usersToCacheByID[] = 'User1'; u::$usersToCacheByID[] = 'User2'; echo 'usersToCacheByID ' . users::$usersToCacheByID . ''; Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712805 Share on other sites More sharing options...
EricIsGood Posted December 11, 2008 Author Share Posted December 11, 2008 Thanks for the reply. I had thought that one of the benifits of a static property was that the class didn't need to be instantiated first? Best, -Eric Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712812 Share on other sites More sharing options...
shlumph Posted December 11, 2008 Share Posted December 11, 2008 Hey, check out this site: http://www.phpbuilder.com/manual/en/language.oop5.static.php Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712821 Share on other sites More sharing options...
EricIsGood Posted December 11, 2008 Author Share Posted December 11, 2008 The class doesn't look static to me. The array is. Sorry if I'm being dense but... I tried typing: static class User { Which gave me a fatal error... According to this page: http://bryanstamour.com/?p=20 I don't need to do anything special to make the class static, so long as it's members are static. Obviously I'm missing something... Thanks for the help, -Eric Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712836 Share on other sites More sharing options...
EricIsGood Posted December 11, 2008 Author Share Posted December 11, 2008 Hey, check out this site: http://www.phpbuilder.com/manual/en/language.oop5.static.php Thanks... following that example, I did: <?php class Foo { public static $my_static = 'foo'; } print Foo::$my_static . "\n"; ?> Which works perfect.... the only difference from my example in my first post seems to be that I'm using an array.... Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712849 Share on other sites More sharing options...
EricIsGood Posted December 11, 2008 Author Share Posted December 11, 2008 Here's another example... example 1 doesn't work example 2 does work.... I'm so confused... <?php //example 1: Class users { public static $usersToCacheByID = array(); } users::$usersToCacheByID[] = 'User1'; users::$usersToCacheByID[] = 'User2'; echo '<pre>usersToCacheByID ' . users::$usersToCacheByID . '</pre>'; //example 2: class Foo { public static $my_static; } Foo::$my_static = 'hi'; echo Foo::$my_static . "\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712855 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 Please learn how to print Arrays out. And yes it did work, you should have mentioned it printed out 'Array'. Class users { public static $usersToCacheByID = array(); } users::$usersToCacheByID[] = 'User1'; users::$usersToCacheByID[] = 'User2'; echo 'usersToCacheByID: '; foreach(users::$usersToCacheByID as $key => $value) { echo 'key: ' . $key . ' value: ' . $value . ' '; } echo ''; Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712868 Share on other sites More sharing options...
EricIsGood Posted December 11, 2008 Author Share Posted December 11, 2008 Please learn how to print Arrays out. And yes it did work, you should have mentioned it printed out 'Array'. Thanks for the help. I was originally using print_r. Between all my moving code arround I lost the print_r call without realizing it. All is good now. Best, -Eric Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712873 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 print_r() dumps out the keys/values, but you have no control, although it's good for debugging. If you want to format them in any way you have use something like a foreach loop. Glad it works now! Quote Link to comment https://forums.phpfreaks.com/topic/136555-solved-need-help-with-a-static-array-please-reproducible-code-included-5-lines/#findComment-712881 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.