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;
}
}