Making an array available to multiple classes.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Making an array available to multiple classes.

Post by social_experiment »

I am creating three different classes to handle 3 database actions (deleting, editing, adding). I've decided to create a multi-dimensional array with all the required fields of each table, fields of each form needed.

Code: Select all

<?php
 $data = array(
'tracks' => array(
'fields'  => array('control_no', 'track_name', 'track_length',     // will be used to create the form, names & id values
  'alias', 'area', 'province', 'country',
  'contact_name', 'contact_number', 'email_address',
  'web_address', 'track_map', 'directions', 'web_page',
),					
'tooltips' => array('Control number', 'Track name', 'Track length (in meters)',     // tool tips for each field
	'Known as', 'Track area', 'Track province',
	'Track country', 'Contact person', 'Contact number',
	'Email address', 'Web address',	'Track map',
	'Directions', 'Web page',
),
// fields for specific table here
),
// etc
?>
The above element is for data related to the "Tracks" option, the application will work with a few "options", tracks, riders to name just two. I would pass an argument to the object and based on this argument create a form for the specific option or create an edit form or delete the record.

I don't want to re-create this array in each of the classes but i'm not sure how to go about making it available to all 3 classes. Defining it as a constant doesn't work, something about scalar values the error message said so i'm thinking of creating a "base" class from which these 3 classes would extend and thus making the property available to all child classes. Is my approach valid or should i take a different route?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Making an array available to multiple classes.

Post by requinix »

I don't get why you want three classes for three actions. And another thing: why does the "delete" class need the full array? As for editing, can you edit all the fields or just a subset?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Making an array available to multiple classes.

Post by social_experiment »

requinix wrote:I don't get why you want three classes for three actions. And another thing: why does the "delete" class need the full array? As for editing, can you edit all the fields or just a subset?
My thought is that it would be easier to manage keeping each actions' code seperate. Re. the delete class - touché, i would only need a few fields there. For editing all fields have to be editable
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply