Jump to content

Data Collision when using Static Class Validation


ponderous2k

Recommended Posts

Hello, my first post here.

 

I created a Validation class that depends entirely on static methods. It appears to be working well, but perhaps I misunderstood exactly the purpose and the consequences of using static methods.

 

My class essentially looks like this:

 

class Validate
{
static public $errors 	= array();
static public $valid  		= array();

static public function Name($name)
{
	if ($name != '')
	{
		self::$valid['name'] = $name;
		return true;
	}
                                else
                                {
	                self::$errors['name'] = 'Name is empty';
	                return false;
                                 }
}
}

 

Does this create any chance whatsoever for a collision of data from multiple users? I am beginning to think it does, simply because from what I have recently learned about static methods, theyr'e essentially global variables because they are not instantiated. If that's the case, then it would seem possible that during times of heavy use, any application depending on this class would confuse submitted data.

 

Any thoughts?

 

Thanks in advance.

Link to comment
Share on other sites

I forgot to state why exactly what I think the problem is.

 

As you can see, the parameters are determined to be either valid or invalid, and are then added to the appropriate static array. After all inputs have been validated and none are determined to be erroneous, the static array $valid is used to pass information onto other classes and the database, like:

 

if (empty(Validate::$errors))
{
    CreateSubscription(Validate::$valid['email']);
}

 

So can the data be corrupted by the time it gets to that point, say, if a third or fourth user were to submit data to the Validate class just before the other user's CreateSubscription method were called?

Link to comment
Share on other sites

Thanks for the reply.

 

OK. So each page request uses a fresh "copy" of the Validate::$valid and Validate::$errors static properties?

 

This topic stems from a question posed by another regarding instantiated VS static classes. The link and offending quote follows:

 

http://stackoverflow.com/questions/1185605/when-to-use-static-vs-instantiated-classes/1185633

 

Another thing about static data is that only one instance of it exists in your program : if you set MyClass::$myData to some value somewhere, it'll have this value, and only it, every where -- Speaking about the user, you would be able to have only one user -- which is not that great, is it ?
Link to comment
Share on other sites

Another thing about static data is that only one instance of it exists in (each invocation of) your program : if you set MyClass::$myData to some value somewhere, it'll have this value, and only it, every where (only in that invocation of your program)

 

Speaking about the user, you would be able to have only one user -- which is not that great, is it ?

^^^ A php variable, an instance of a class or  static class properties or methods, a static variable in a function or in a class, database connections, file handles, ... are ALL resources that are created when a .php page is executed. They are all destroyed when the server finishes processing that instance of that .php page.

 

Otherwise, server side scripting would be impossible and you could never have more than one visitor requesting the same page at the same time.

 

You must actually take specific steps in your scripts to get information to pass between any two page requests. For one specific visitor you must use either cookies/sessions, pass values through the URL, or pass information as POST data from a form to the next page requested. For different visitors you must store the shared information in a database or in specific shared memory (requires a shared memory driver to be installed on the server.)

 

Link to comment
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.