ponderous2k Posted August 15, 2010 Share Posted August 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/ Share on other sites More sharing options...
ponderous2k Posted August 15, 2010 Author Share Posted August 15, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099600 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 All resources used on a page request are destroyed when the processing of that page request completes. Each visitor and each request for the same page by the same visitor is completely separate from all the other requests that the server gets. Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099610 Share on other sites More sharing options...
ponderous2k Posted August 15, 2010 Author Share Posted August 15, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099655 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099661 Share on other sites More sharing options...
ponderous2k Posted August 16, 2010 Author Share Posted August 16, 2010 Yes, this concept makes sense, and it's what I've based all my programming around thus far in my career. But for some reason I found myself doubting suddenly the reliability of my data. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099936 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2010 Share Posted August 16, 2010 they're essentially global variables... That's correct, but any "global variables" in a script only exist within that instance of that php script. Quote Link to comment https://forums.phpfreaks.com/topic/210798-data-collision-when-using-static-class-validation/#findComment-1099938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.