Jump to content

singleton inside functions


phpJoeMo

Recommended Posts

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','asdf@asdf.asd').$br;
echo clean_it('asdfsdf.com','domain');

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.