Jump to content

EricIsGood

New Members
  • Posts

    8
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

EricIsGood's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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
  2. 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"; ?>
  3. 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....
  4. 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
  5. 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
  6. <?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
  7. Chris, Thanks for the reply. This works fine in my current case. For future reference, if I had a very large collection of objects, and wanted to let external code iterate through them, would this be substantially slower than direct access to an array? Thanks, -Eirc
  8. I've been googling for hours, and I can't seem to find any 'best practice' information on this. In my example, I have a Notice object, and a Notices object. Notices is simply a collection of Notice objects. In the future I may want to have more control over adding / removing notices from the collection, but for now the raw access is fine. My example code is bellow. My questions are: 1. is this the best way to go about a collection of objects? 2. if I want more control over Notices (plural) such as restricting the type of Notice (singular) that can be added to the collection, could I do that without refactoring code outside the class? 3. would it be better design to only allow the Notices class to access the Notice class? Thanks for your help. -Eric $notices = new Notices(); if (trim($userName) == "") { $notice = new Notice('userName','Please choose a username.'); $this->notices->notice[0] = $notice; } if (trim($userName) == "Webmaster") { $notice = new Notice('userName','Reserved Name.'); $this->notices->notice[1] = $notice; } echo $this->notices->notice[0]->text; echo $this->notices->notice[1]->text; class Notices { public $notice = array(); } class Notice { public $type; public $text; public function __construct($type, $text) { $this->type = $type; $this->text = $text; } }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.