phpJoeMo Posted May 11, 2011 Share Posted May 11, 2011 OK, so I'm trying to create a central db class for data validation, etc. I'm starting off with the data. this code works for me, I just want to be sure that I'm not missing anything. Will dbData be accessible globally? class dbData{ function regex_key_data($reg){ $z = '/[a-z\s\'{1}]*/'; $d= array( 'domain'=>'/^[a-z0-9-.]+\.[a-z0-9]{2,4}$/i', 'email'=>'/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]+$/i', 'GUID'=>'/[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}/', 'html'=>'*htmlentities', 'int'=>'[0-9]', 'string'=>'*mysql_real_escape_string', 'fname'=>$z, 'lname'=>$z ); return $d[$reg]; } function test($reg,$val){ $z = preg_match(dbData::regex_key_data($reg),$val ,$dom); return ($z?$dom[0]:false); } } function clean_it($v,$type = 'none'){ $v = urldecode($v); if(!empty($v)){ switch($type){ case 'domain': return dbData::test('domain',$v); break; case 'email': preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]+$/i', $v,$email); if(empty($email[0])){return 'empty';} else{return mysql_real_escape_string($email[0]);} break; case 'GUID': return mysql_real_escape_string($v); break; case 'html': return mysql_real_escape_string(htmlentities($v)); break; case 'int': if(is_numeric($v)){return $v;} break; case 'string': return mysql_real_escape_string($v); break; } } else{ switch($type){ case 'domain': case 'none': case 'string': case 'email': case 'GUID': case 'html': return 'empty'; break; case 'int': return 0; break; } } } $br = '<br/>'; echo dbData::test('GUID','ASDFASDF-ASDF-ASDF-ASDF-ASDFASDFASDF').$br; echo dbData::test('email','[email protected]').$br; echo clean_it('asdfsdf.com','domain'); Quote Link to comment https://forums.phpfreaks.com/topic/236127-singleton-inside-functions/ Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 You should define your methods as static, if they are to be used statically public static function test () {} As long as you aren't referencing anything as an object within your class though, you should be fine. Edit - rather than use dbData::Method WITHIN your class, you should use self::Method Quote Link to comment https://forums.phpfreaks.com/topic/236127-singleton-inside-functions/#findComment-1213995 Share on other sites More sharing options...
phpJoeMo Posted May 11, 2011 Author Share Posted May 11, 2011 Thanks for the advice. I'm assuming that your are referring to php5 best practices by using public static? Is this mainly for code readability? I'm juggling between my hosting which uses php4 and zend php cert. 5.3 that I'm studying for. Quote Link to comment https://forums.phpfreaks.com/topic/236127-singleton-inside-functions/#findComment-1214008 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 Calling non-static methods statically generates an E_STRICT level warning. It's technically wrong the way you have it, but PHP allows it for some reason. Possibly backwards-compatibility. Read up more here http://www.php.net/manual/en/language.oop5.static.php Quote Link to comment https://forums.phpfreaks.com/topic/236127-singleton-inside-functions/#findComment-1214015 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.