Encapsulation of public resource classes

Source: Internet
Author: User

<?PHP/** * Public Resources class * * **/classCommon {/** * General JSON return data * $code int returns an error code * $MSG string returns an error message * $data array returned by*/     Public Staticfunction Echoresult ($code =0, $msg ="', $data =Array ()) {$result=Array ('Code'=$code,'msg'=$msg,'Data'=$data);        Echo Json_encode ($result, Json_unescaped_unicode);    Exit }    /** * processing parameter filter For form submission * $string string or array to be processed by string * @return string returns the string or array after processing*/     Public StaticFunction Dtrim ($string){        if(Is_array ($string) {$keys= Array_keys ($string); foreach($keys as$key) {$val= $string[$key]; Unset ($string[$key]); $string[Trim ($key)] =Self ::D trim ($val); }        } Else {            $string= Trim ($string); }        return$string; }    /** * Processing the parameter filter for form submission * $string string or array to be processed $force Boolean forced to process * @return string returned after processing String or array*/     Public StaticFunction Daddslashes ($string, $force =1) {        if(Is_array ($string) {$keys= Array_keys ($string); foreach($keys as$key) {$val= $string[$key]; Unset ($string[$key]); $string[Addslashes ($key)] =Self ::d addslashes ($val, $force); }        } Else {            $string= Addslashes ($string); }        return$string; }    //please refer to PHP built-in methods Htmlspecialchars     Public StaticFunction Dhtmlspecialchars ($string, $flags =Ent_compat) {        if(Is_array ($string)) {            foreach($string  as$key =$val) {                $string[$key] =Self ::d htmlspecialchars ($val, $flags); }        } Else {            if($flags = = =NULL) {                $string= Str_replace (Array ('&','"','<','>'), Array ('&amp;','&quot;','&lt;','&gt;'), $string); if(Strpos ($string,'&amp;#') !==false) {                    $string= Preg_replace ('/&amp; ((# (\d{3,5}|x[a-fa-f0-9]{4}));)/','&\\1', $string); }            } Else {                if(Php_version <'5.4.0') {                    $string= Htmlspecialchars ($string, $flags); } Else{$charset='UTF-8'; $string= Htmlspecialchars ($string, $flags, $charset); }            }        }        return$string; }    //please refer to PHP built-in methods Stripslashes     Public StaticFunction Dstripslashes ($string) {        if(Empty ($string))return$string; if(Is_array ($string)) {            foreach($string  as$key =$val) {                $string[$key] =Self ::d stripslashes ($val); }        } Else {            $string= Stripslashes ($string); }        return$string; }    /** Function Name: Verifyid () * Function: Verify the Submitted ID class value is valid * parameter: $id: The ID value of the submitted * return value: Returns the processed ID **/     Public Staticfunction Verifyid ($id =NULL) {        if(!$id) {            return 0; } //is null-judgedElseIf (Self::inject_check ($id)) {return 0; } //Injection JudgmentElseIf (!is_numeric ($id)) {            return 0 ; } //Digital Judgment$id = Intval ($id);//The whole type of        return$id; }    /** Detection of submitted values is not a character that contains SQL injections, prevents injections, protects server security * Parameters: $sql _STR: Committed variable * return value: Return test result, ture or false*/     Public Staticfunction Injectcheck ($sql _str) {return@eregi ('select|insert|and|or|update|delete|\ ' |\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql _str);//to filter    }                            /** * Import files and instantiate, the default import is business this folder under the file * $filename string $layer model layer Name * Return Object*/     Public Staticfunction importbusiness ($name =NULL) {$layer=" Business"; $class=$name. Ucfirst ($layer); Static$_obj=Array (); if(Isset ($_obj[$name])) {return$_obj[$name]; }        if(!file_exists (App_path."/application/{$layer}/{$class}.php") ) {exit ("file {$class}.php is not exists"); } yaf_loader::import (App_path."/application/business/{$class}.php");//Import the class library (Business logic class library, etc.)$_obj[$name] =New$class(); return$_obj[$name]; }            /** * Import files and instantiate, the default import is business this folder under the file * $filename string $layer model layer Name * Return Object*/     Public Staticfunction importmodulesbusiness ($name =NULL, $modules =NULL) {$layer=" Business"; $class=$name. Ucfirst ($layer); Static$_obj=Array (); if(Isset ($_obj[$name])) {return$_obj[$name]; }        if(!file_exists (App_path."/application/modules/{$modules}/{$layer}/{$class}.php") ) {exit ("file {$class}.php is not exists"); } yaf_loader::import (App_path."/application/modules/{$modules}/business/{$class}.php");//Import the class library (Business logic class library, etc.)$_obj[$name] =New$class(); return$_obj[$name]; }            }

Encapsulation of public resource classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.