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','[email protected]').$br;
echo clean_it('asdfsdf.com','domain');

Link to comment
https://forums.phpfreaks.com/topic/236127-singleton-inside-functions/
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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.