Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4570 > unrolled thread

Loading globals into classes

Started byScott Johnson <noonehome@chalupasworld.com>
First post2012-01-22 09:20 -0800
Last post2012-01-22 17:10 -0800
Articles 6 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Loading globals into classes Scott Johnson <noonehome@chalupasworld.com> - 2012-01-22 09:20 -0800
    Re: Loading globals into classes "J.O. Aho" <user@example.net> - 2012-01-22 21:09 +0100
      Re: Loading globals into classes Scott Johnson <noonehome@chalupasworld.com> - 2012-01-22 14:08 -0800
      Re: Loading globals into classes Scott Johnson <noonehome@chalupasworld.com> - 2012-01-22 14:29 -0800
        Re: Loading globals into classes "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-23 00:32 +0100
          Re: Loading globals into classes Scott Johnson <noonehome@chalupasworld.com> - 2012-01-22 17:10 -0800

#4570 — Loading globals into classes

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-01-22 09:20 -0800
SubjectLoading globals into classes
Message-ID<jfhghn$rrn$1@dont-email.me>
I have arrays set up in a separate included file and would like to 
access them in a class.

I have a working solution but am wondering if there is not a better 
approach more practical for this.

What I have that works is as follows (so far):

		private $uom_ary;
		private $months_ary;

		public function __construct()
		{
			global $uom_ary;
			global $months_ary;
			$this->uom_ary = $uom_ary;
			$this->months_ary = $months_ary;
		}

Previously I just used the global at the top of each method but had to 
add it for each method needing the array.

Any ideas an a better approach and the pitfalls to what I am currently 
using would be appreciative.

Thanks
Scotty

[toc] | [next] | [standalone]


#4571

From"J.O. Aho" <user@example.net>
Date2012-01-22 21:09 +0100
Message-ID<9o38ofFagkU1@mid.individual.net>
In reply to#4570
Scott Johnson wrote:

> public function __construct($uom_ary, $months_ary)
> {
> $this->uom_ary = $uom_ary;
> $this->months_ary = $months_ary;
> }
>
> Previously I just used the global at the top of each method but had to add it
> for each method needing the array.

Why not supply the information as arguments for the constructor.

-- 

   //Aho

[toc] | [prev] | [next] | [standalone]


#4576

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-01-22 14:08 -0800
Message-ID<jfi1d7$54p$1@dont-email.me>
In reply to#4571
On 1/22/2012 12:09 PM, J.O. Aho wrote:
> Scott Johnson wrote:
>
>> public function __construct($uom_ary, $months_ary)
>> {
>> $this->uom_ary = $uom_ary;
>> $this->months_ary = $months_ary;
>> }
>>
>> Previously I just used the global at the top of each method but had to
>> add it
>> for each method needing the array.
>
> Why not supply the information as arguments for the constructor.
>

Thanks for the input.

I will do some research on doing just that.  Kind of like, 'you don't 
know what you don't know' ;)

[toc] | [prev] | [next] | [standalone]


#4577

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-01-22 14:29 -0800
Message-ID<jfi2kh$ege$1@dont-email.me>
In reply to#4571
On 1/22/2012 12:09 PM, J.O. Aho wrote:
> Scott Johnson wrote:
>
>> public function __construct($uom_ary, $months_ary)
>> {
>> $this->uom_ary = $uom_ary;
>> $this->months_ary = $months_ary;
>> }
>>
>> Previously I just used the global at the top of each method but had to
>> add it
>> for each method needing the array.
>
> Why not supply the information as arguments for the constructor.
>

That seemed to work pretty well and seemed a bit less messy

Here is what I did:

In my array included file:

$months_ary = array(
	'1'=>'January',
	'2'=>'February',
	'3'=>'March',
	'4'=>'April',
	'5'=>'May',
	'6'=>'June',
	'7'=>'July',
	'8'=>'August',
	'9'=>'September',
	'10'=>'October',
	'11'=>'November',
	'12'=>'December');

$uom_ary = array(
	'Each'=>'Each',
	'Dozen'=>'Dozen');
	
$global_arys = array(
	'Months'=>$months_ary,
	'UOM'=>$uom_ary);

Then I instantiated the class:

$processing_cl = new processing_class($global_arys);

I can then pull the arrays out of the construct with func_get_args()

Does that seem like a decent approach.  Maybe I am over complicating 
this at this time, but it is kind of good to know different ways of 
approaching different situations if needed.

Thanks again for idea.

[toc] | [prev] | [next] | [standalone]


#4579

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-01-23 00:32 +0100
Message-ID<9o3kkoF541U1@mid.uni-berlin.de>
In reply to#4577
Am 22.01.2012 23:29, schrieb Scott Johnson:
> On 1/22/2012 12:09 PM, J.O. Aho wrote:
>> Scott Johnson wrote:
>>
>>> public function __construct($uom_ary, $months_ary)
>>> {
>>> $this->uom_ary = $uom_ary;
>>> $this->months_ary = $months_ary;
>>> }
>>>
>>> Previously I just used the global at the top of each method but had to
>>> add it
>>> for each method needing the array.
>>
>> Why not supply the information as arguments for the constructor.
>>
> 
> That seemed to work pretty well and seemed a bit less messy
> 
> Here is what I did:
> 
> In my array included file:
> 
> $months_ary = array(
>     '1'=>'January',
>     '2'=>'February',
>     '3'=>'March',
>     '4'=>'April',
>     '5'=>'May',
>     '6'=>'June',
>     '7'=>'July',
>     '8'=>'August',
>     '9'=>'September',
>     '10'=>'October',
>     '11'=>'November',
>     '12'=>'December');
> 
> $uom_ary = array(
>     'Each'=>'Each',
>     'Dozen'=>'Dozen');
>     
> $global_arys = array(
>     'Months'=>$months_ary,
>     'UOM'=>$uom_ary);
> 
> Then I instantiated the class:
> 
> $processing_cl = new processing_class($global_arys);
> 
> I can then pull the arrays out of the construct with func_get_args()
> 
> Does that seem like a decent approach.  Maybe I am over complicating this at this
> time, but it is kind of good to know different ways of approaching different
> situations if needed.
> 
> Thanks again for idea.
> 

Consider putting the whole array into the class without defining it outside, and put
your code questioning it into the class.

/Str.

[toc] | [prev] | [next] | [standalone]


#4582

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-01-22 17:10 -0800
Message-ID<jfic32$pg$1@dont-email.me>
In reply to#4579
On 1/22/2012 3:32 PM, M. Strobel wrote:
> Am 22.01.2012 23:29, schrieb Scott Johnson:
>> On 1/22/2012 12:09 PM, J.O. Aho wrote:
>>> Scott Johnson wrote:
>>>
>>>> public function __construct($uom_ary, $months_ary)
>>>> {
>>>> $this->uom_ary = $uom_ary;
>>>> $this->months_ary = $months_ary;
>>>> }
>>>>
>>>> Previously I just used the global at the top of each method but had to
>>>> add it
>>>> for each method needing the array.
>>>
>>> Why not supply the information as arguments for the constructor.
>>>
>>
>> That seemed to work pretty well and seemed a bit less messy
>>
>> Here is what I did:
>>
>> In my array included file:
>>
>> $months_ary = array(
>>      '1'=>'January',
>>      '2'=>'February',
>>      '3'=>'March',
>>      '4'=>'April',
>>      '5'=>'May',
>>      '6'=>'June',
>>      '7'=>'July',
>>      '8'=>'August',
>>      '9'=>'September',
>>      '10'=>'October',
>>      '11'=>'November',
>>      '12'=>'December');
>>
>> $uom_ary = array(
>>      'Each'=>'Each',
>>      'Dozen'=>'Dozen');
>>
>> $global_arys = array(
>>      'Months'=>$months_ary,
>>      'UOM'=>$uom_ary);
>>
>> Then I instantiated the class:
>>
>> $processing_cl = new processing_class($global_arys);
>>
>> I can then pull the arrays out of the construct with func_get_args()
>>
>> Does that seem like a decent approach.  Maybe I am over complicating this at this
>> time, but it is kind of good to know different ways of approaching different
>> situations if needed.
>>
>> Thanks again for idea.
>>
>
> Consider putting the whole array into the class without defining it outside, and put
> your code questioning it into the class.
>
> /Str.

Actually you bring up a good point.  I have a static form class for 
validating and displaying form elements.  It would make sense to place 
the definitions in there.

I do however like the idea of having a simple flat file to edit as the 
client needs.

I used to have them in a DB but it seemed a little much.

Well either way I go, you all have given me some good food for thought.

Thanks for your time.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web