FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Loading globals into classes
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Loading globals into classes [message #176759] Sun, 22 January 2012 17:20 Go to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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
Re: Loading globals into classes [message #176760 is a reply to message #176759] Sun, 22 January 2012 20:09 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
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
Re: Loading globals into classes [message #176765 is a reply to message #176760] Sun, 22 January 2012 22:08 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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' ;)
Re: Loading globals into classes [message #176766 is a reply to message #176760] Sun, 22 January 2012 22:29 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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.
Re: Loading globals into classes [message #176768 is a reply to message #176766] Sun, 22 January 2012 23:32 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: Loading globals into classes [message #176771 is a reply to message #176768] Mon, 23 January 2012 01:10 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: list() Struktur auslagern
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 20:45:15 GMT 2024

Total time taken to generate the page: 0.02642 seconds