CodeIgniter source reading notes (3) - public function Common.php

1. Overview

Common declares many functions of common operations. Common.php is recorded in Codeigniter.php, so this file is called globally.

1. What functions does Common provide?

is_php: determine PHP version

    function is_php($version)
    {
        //Note that instead of returning true or false directly, the comparison results are placed in the static variable is_php,
        //This is mainly to prevent repeated comparison
        static $_is_php;
        $version = (string) $version;

        if ( ! isset($_is_php[$version]))
        {
            $_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
        }

        return $_is_php[$version];
    }

Is? Really? Writable: is the file writable? Here we need to consider a question: how to determine whether the folder has write permission?

        if (is_dir($file))
        {
            //Try to create a random string file. If the creation fails, it is not writable. The creation succeeds,
            //Delete random file, return true, writable
            $file = rtrim($file, '/').'/'.md5(mt_rand());
            if (($fp = @fopen($file, 'ab')) === FALSE)
            {
                return FALSE;
            }

            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
            return TRUE;
        }

&Load_class: load class, pointer function, see how ci loads class

    function &load_class($class, $directory = 'libraries', $param = NULL)
    {
        //Save static array of loading class
        static $_classes = array();
        //If it has been loaded, return directly
        if (isset($_classes[$class]))
        {
            return $_classes[$class];
        }
        $name = FALSE;
        //Load priority apppath > basepath
        foreach (array(APPPATH, BASEPATH) as $path)
        {
            if (file_exists($path.$directory.'/'.$class.'.php'))
            {
                $name = 'CI_'.$class;
                if (class_exists($name, FALSE) === FALSE)
                {
                    require_once($path.$directory.'/'.$class.'.php');
                }
                break;
            }
        }
        //Does the file with custom prefix exist
        if (file_exists(APPPATH.$directory.'/'
                .config_item('subclass_prefix').$class.'.php'))
        {
            $name = config_item('subclass_prefix').$class;

            if (class_exists($name, FALSE) === FALSE)
            {
                require_once(APPPATH.$directory.'/'.$name.'.php');
            }
        }
        //If file not found, respond, 503
        if ($name === FALSE)
        {
            set_status_header(503);
            echo 'Unable to locate the specified class: '.$class.'.php';
            exit(5); // EXIT_UNK_CLASS
        }
        //Add to loaded array
        is_loaded($class);
        //Instantiate the load class and store the instance in the $'u classes array
        $_classes[$class] = isset($param)
            ? new $name($param)
            : new $name();
        return $_classes[$class];
    }
&Is? Loaded: add the loaded class to the loaded list
&Get config: load the config.php configuration file and return. There is an optional array parameter $replace. If the parameter is not empty, add the array to the configuration
Config item: returns the specified configuration item
&Get umes: returns an array of MIME types from config / mime.php
Is https: is it https
Is cli: is it cli
Show error: show error page
Show 404: 404 page processing
Log? Message: error log
Set status header: set response header status
_Error? Handler: error handling
_Exception "handler: exception handling
_Shutdown? Handler: turns off exception and error handling
Remove ABCD invisible ABCD characters: remove invisible characters (this feature is useful when working with strings)
html escape: escaping a string into an html statement
_stringify_attributes: used to convert string, array, object to string
Function? Available

Tags: PHP

Posted on Sat, 02 May 2020 17:00:06 -0700 by Daukan