Jump to content

Recommended Posts

Is there any way to clean up this function?

 

function foo()
{
     if (isset($jack)) {$_SESSION['jack'] = serialize($jack);}
     if (isset($bill)) {$_SESSION['bill'] = serialize($bill);}
     if (isset($joe)) {$_SESSION['joe'] = serialize($joe);}
     if (isset($fred)) {$_SESSION['fred'] = serialize($fred);}
     //et cetera... total of 500 such lines
}

 

I've been trying to somehow use an array with (jack, bill, joe, fred), but I can't seem to figure out how.

Link to comment
https://forums.phpfreaks.com/topic/83372-really-ugly-code/
Share on other sites

As your function stands now, none of the isset() will return "true", since those variables are all local to the function and haven't been set in the function.

 

That being said, what are the variables? Where are they coming from? Also, you don't have to use serialize when storing values in the $_SESSION array, since PHP will do that for you.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/83372-really-ugly-code/#findComment-424159
Share on other sites

If the values are already in an array, you can assign that array directly to a session variable, no need for a lot of code.

 

However, I would ask, why are you attempting to put 500 values into a session? Given that the purpose of a session is to pass variables that exist on one page to another page (or a refresh of the same page), it is unlikely that you have 100's of values that are generated or input on one page that need to be passed to another page. I would guess that you really should be using a single page or using a database and just query for the information that you want.

 

If you describe what your application is and what you are trying to accomplish, you can probably get a better solution to the problem.

Link to comment
https://forums.phpfreaks.com/topic/83372-really-ugly-code/#findComment-424251
Share on other sites

If the values are already in an array, you can assign that array directly to a session variable, no need for a lot of code.

 

However, I would ask, why are you attempting to put 500 values into a session? Given that the purpose of a session is to pass variables that exist on one page to another page (or a refresh of the same page), it is unlikely that you have 100's of values that are generated or input on one page that need to be passed to another page. I would guess that you really should be using a single page or using a database and just query for the information that you want.

 

If you describe what your application is and what you are trying to accomplish, you can probably get a better solution to the problem.

 

I'm not trying to put 500 variables into a session.  At any given time, there are 3-4 objects that I need to use in my script.  I use sessions to store these objects, as they are temporary.  I cannot use an object when it is serialized, and the session refuses to store it unserialized.  Therefore, I must unserialize the 3-4 objects at the beginning of the script's execution, and then serialize them again at the end.

 

I find it very tedious to write the code for this manually (fairly large script with 20+ files and counting), so I have the files serialize.php and unserialize.php.  The code I have posted is an excerpt from serialize.php, which checks each (serialized) variable, and unserializes it if it exists.  Because there are 500 different possible objects which I might need to store, I need 500 lines of code to test each one.

 

@Barand:  Thanks, I'll try that tomorrow.

Link to comment
https://forums.phpfreaks.com/topic/83372-really-ugly-code/#findComment-424463
Share on other sites

If your object is part of a class, as long as your class definition is present before the session_start() on any page, you can create an instance of that class in a session and reference it on any page. The following is working code -

 

page1 -

 

<?php
require("class.inc.php"); // class definition
session_start(); // start or resume session
$_SESSION['your_object'] = new Veg(); // create the class object in the session
$_SESSION['your_object']->Vegetable("turnip", "white"); // set a class member
echo $_SESSION['your_object']->what_color(); // display a class member
?>

 

page2 -

 

<?php
require("class.inc.php"); // class definition
session_start(); // start or resume the session
echo $_SESSION['your_object']->what_color(); // display a class member
?>

 

If there should be some reason why you cannot do the above, you need to use an array of objects then just use a foreach() loop to iterate over the array. There is no need to write out a line of code for each possible named object. That is what loops are for.

 

Again, if you give enough details about what you are trying to accomplish, someone can provide an easy way of doing it.

Link to comment
https://forums.phpfreaks.com/topic/83372-really-ugly-code/#findComment-424472
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.