Forum Moderators: coopster
I've got a number of arrays in an application that I use to initialize and re-initialize a bunch of session vars.
The most efficient way to do this (I think) would be to "globalise" each array once and then call/re-call the initialize function any time I'd need to have them re-initialized.
I'm obviously doing something wrong however, as my script is reporting that it can't see the globals I'm passing to my function.
Here's what I have so far:
//create globals
global $vesselSessions, $positionSessions;
//feed arrays into globals
$vesselSessions = array(
'Commercial','Yacht','None','VesselExp');
$positionSessions = array(
'Captain','First','Second','Third','Bosun','Engineer','Deck','Steward',
'Stewardess','Cook','Laundry','Mechanic','Masseuse','Beautician','Position');
//call the function that initializes the arrays
initSessions($vesselSessions);
initSessions($positionSessions);
//function that initializes the arrays
function initSessions($sessionList)
{
foreach($sessionList as $key)
{
$_SESSION[$key] = 0;
}
}
Can anyone see where I'm going wrong? Is there a better way to do this? I've read on-line that some people think that using objects is a better solution to this type of problem but I've never done any php object programming yet.
All guidance and opinions greatly apprecitated.
Neophyte
Thanks for your answer.
Yes, upon further reading, I think that you're generally correct, but that makes my function a "one trick pony" that can only do something with vesselSessions and nothing else.
Isn't there a way to "pass" a global var to a function? Or is Habtom correct that you must declare the global inside the function itself - which would essentially restrict that functions use to one var?
I ***thought*** that one could create like an "init.php" file in which you could define any globals you wanted for use throughout the application. This is really where I'm trying to go with this question as I often find myself having to re-declare the same vars/arrays/whatever throughout the lifecycle of a script when I think there is probably a technique to do it just once - that technique I thought would be globals but perhaps I'm incorrect.
Anyone else?
Neophyte
init.php
$vesselSessions["var1"] = "Commercial";
$vesselSessions["var2"] = "Yacht";
$vesselSessions["var3"] = "None";
$vesselSessions["var4"] = "VesselExp";$positionSessions["var1"] = "Captain";
$positionSessions["var2"] = "First";
$positionSessions["var3"] = "Second";
$positionSessions["var4"] = "Third";
ect, etc...
page.php
include "init.php";function foo()
{
global $vesselSessions, $positionSessions;
// do whatever you need in the function.
// to call a variable...
echo ($vesselSessions["var2"]); // outputs: Yacht
}
Sorry if I'm way off on what you want to achieve!
[edited by: Borgscan at 9:08 am (utc) on June 17, 2007]
Thanks for your reply, but this isn't what I'm looking for. Maybe the core of my question isn't clear; let me try again:
During the development of an application, we all frequently find discover there are things (variables with db settings, arrays, etc) that would be useful to ACCESS multiple times and from anywhere in the application. Each time we need these items we could, of course, re-initialize these items as needed, but sometimes it would be nice to initialize them only once, and then grab them as needed.
Such is my desire with these arrays which build session variables.
So, what I'd like to know is if I can initialize an array of strings, such as:
$positionSessions = array(
'Captain','First','Second','Third','Bosun','Engineer','Deck','Steward',
'Stewardess','Cook','Laundry','Mechanic','Masseuse','Beautician','Position');
Which would then be available to me at any time I'd want it - and from anywhere in the application - without the need to re-initialize it.
So, if I wanted to build a bunch of session vars from this array - or any other that was already "there", all that I'd have to do is call a function like this:
function initSessions($sessionList)
{
foreach($sessionList as $key)
{
$_SESSION[$key] = 0;
}
}
In specific, I'm using this and other arrays for data validation purposes and there are times during the data validation process where I need to reset all items in the session list to zero in the event that the user changes their mind about what they've selected or input, but the core of my question involves being able to initialize an item once and re-use it and/or alter it's contents any time I'd like WITHOUT having to declare
$positionSessions = array(
'Captain','First','Second','Third','Bosun','Engineer','Deck','Steward',
'Stewardess','Cook','Laundry','Mechanic','Masseuse','Beautician','Position');
...again, and again, and again.
Since PHP has a "global" keyword, I thought that it could be done that way as if my $positionsSessions array were global, would always be there for me to use without the need to re-declare it.
So, I thought that doing:
global $positionSessions;
$positionSessions = array(
'Captain','First','Second','Third','Bosun','Engineer','Deck','Steward',
'Stewardess','Cook','Laundry','Mechanic','Masseuse','Beautician','Position');
would make this array available to me at any time in the application, but when I call my function with the array name...
initSessions($positionSessions);
I can't seem to get my function...
function initSessions($sessionList)
{
foreach($sessionList as $key)
{
$_SESSION[$key] = 0;
}
}
to see this "global" array.
That's about as clear as I can be about it.
Is there anyway to do this?
Neophyte
I am in a similar situation wherein I do not want call a global in every function. Neither do I want to enable superglobals due to the myriad security issues.
From what I've researched, it seems classes and objects are the solution.
I am not sure yet, how passing a variable to every function is different from creating objects in classes. Maybe someone could through light into this.
From my continuing research I'm starting to come to the same conclusion - that creating objects would be the solution to my (our) query on this subject - the issue of course is persistence. If, in my case, I create object arrays, will they "persist" page to page like sessions do.
If someone could answer, "yes they will" then I've got my solution ... however, it also means that I've got to start getting my hands dirty with php/oop which I've always cringed at the thought, but may now is the time.
would appreciate anyone's thoughts on the issue of objects "persisting" until they're unset or destroyed.
Thanks to all.
Neophyte
From some site
When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using PHP sessions
[edited by: jatar_k at 12:38 pm (utc) on June 20, 2007]
[edit reason] no urls thanks [/edit]