Dynamic Includes in PHP

Discussion in 'PHP' started by pein87, May 29, 2012.

  1. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    Allows you to include dynamically a single file, a list of files or a whole directory. No more tedious include statements.

    Background



    This is designed to be similar to Java's package system but instead uses a directory, file/s or */ALL, and include type(INC, IN_O, REQ, REQ_O). The reason is it can become a hassle if you want to include a bunch of files or templates for smarty or phar files in your code. Now its possible to include all of these with 4 lines of code.

    The Code



    Dynamic include of phar,tpl, and php files in your projects. uses a predefined class which implements an interface for those who'd like to create their own version.

    Import class name: class_import.php

    PHP:
    <?php

    /***************************************\
    # Created By: Rickey Otano a.k.a pein87 #
    # License: Creative Commons 2 BSD       #
    # Creation Date: 5/29/12                #
    # Usage: Java-ish package system for    #
    # dynamic includes of files from a      #
    # folder. Include single files          #
    # list of files, or whole folders       #
    # I am not at fault for any misuse,     #
    # damage or issues this may create      #
    # and this is provided as is with       #
    # no guarentees of any kind             #
    \***************************************/

        /* use interface so others can implement it their own way */
        
    interface packages
        
    {
            
    /* function that includes php files */
            
    public function package($dir,$files,$type);
            
            
    /* function that include tpl */
            
    public function packageTpl($dir,$files,$type);
            
            
    /* function that includes phar files */
            
    public function packagePhar($dir,$files,$type);
        }
        
        class 
    import implements packages
        
    {
        
            public function 
    package($dir,$files,$type)
            {
                
    /* arrays of required values or expected values of $files and $type */
                
    $fileField = array("*","ALL"); // inlcude all files if $files equals on of them
                
    $typeField = array("INC","REQ","INC_O","REQ_O"); // type of include to perform
                
                /* dir and file handles */
                
    $dirHandle opendir($dir); // directory handle(opens dir to reading)
                
    $resources// blank variable that will hold all the resources
                
    $dirLen strlen($dir); // number of characters in the directory text
                
                /* check if dir has / added at the end or not and if not add it*/
                
    if(substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) == "/")
                {
                    
    $dir $dir "/"// add / to dir path's end
                
    }
                else if(
    substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) !== "/")
                {
                    
    $dir "/" $dir "/"// add / to dir path on front and back
                
    }
                else
                {
                    
    /* correct value is there so do nothing */
                
    }
                
                if(
    $dirHandle)
                {
                    
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 4$rLen) == ".php")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".php")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($dir $files[$i] . ".php");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($dir $files[$i] . ".php");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($dir $files[$i] . ".php");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($dir $files[$i] . ".php");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($dir $files[$i] . ".php");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($dir $files ".php"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($dir $files ".php");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($dir $files ".php");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($dir $files ".php");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($dir $files ".php");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($dir $files ".php");
                                break;
                            }
                        }
                    }
                }
                else if(
    $dir == "" || $dir == NULL)
                {
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
    $dirHandle opendir(getcwd()); // current folder in use so get current working folder
                    
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 4$rLen) == ".php")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".php")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($files[$i] . ".php");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($files[$i] . ".php");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($files[$i] . ".php");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($files[$i] . ".php");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($files[$i] . ".php");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($files ".php"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($files ".php");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($files ".php");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($files ".php");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($files ".php");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($files ".php");
                                break;
                            }
                        }
                    }
                }
            
            }
            
            public function 
    packageTpl($dir,$files,$type)
            {
                
    /* arrays of required values or expected values of $files and $type */
                
    $fileField = array("*","ALL"); // inlcude all files if $files equals on of them
                
    $typeField = array("INC","REQ","INC_O","REQ_O"); // type of include to perform
                
                /* dir and file handles */
                
    $dirHandle opendir($dir); // directory handle(opens dir to reading)
                
    $resources// blank variable that will hold all the resources
                
    $dirLen strlen($dir); // number of characters in the directory text
                
                /* check if dir has / added at the end or not and if not add it*/
                
    if(substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) == "/")
                {
                    
    $dir $dir "/"// add / to dir path's end
                
    }
                else if(
    substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) !== "/")
                {
                    
    $dir "/" $dir "/"// add / to dir path on front and back
                
    }
                else
                {
                    
    /* correct value is there so do nothing */
                
    }
                
                if(
    $dirHandle)
                {
                    
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 4$rLen) == ".tpl")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".tpl")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($dir $files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($dir $files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($dir $files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($dir $files[$i] . ".tpl");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($dir $files[$i] . ".tpl");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($dir $files ".tpl"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($dir $files ".tpl");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($dir $files ".tpl");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($dir $files ".tpl");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($dir $files ".tpl");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($dir $files ".tpl");
                                break;
                            }
                        }
                    }
                }
                else if(
    $dir == "" || $dir == NULL)
                {
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
                    
    $dirHandle opendir(getcwd()); // current folder in use so get current working folder
                    
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 4$rLen) == ".tpl")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".tpl")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($files[$i] . ".tpl");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($files[$i] . ".tpl");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($files[$i] . ".tpl");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($files ".tpl"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($files ".tpl");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($files ".tpl");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($files ".tpl");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($files ".tpl");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($files ".tpl");
                                break;
                            }
                        }
                    }
                }
            
            }
            
            
            public function 
    packagePhar($dir,$files,$type)
            {
                
    /* arrays of required values or expected values of $files and $type */
                
    $fileField = array("*","ALL"); // inlcude all files if $files equals on of them
                
    $typeField = array("INC","REQ","INC_O","REQ_O"); // type of include to perform
                
                /* dir and file handles */
                
    $dirHandle opendir($dir); // directory handle(opens dir to reading)
                
    $resources// blank variable that will hold all the resources
                
    $dirLen strlen($dir); // number of characters in the directory text
                
                /* check if dir has / added at the end or not and if not add it*/
                
    if(substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) == "/")
                {
                    
    $dir $dir "/"// add / to dir path's end
                
    }
                else if(
    substr($dir,$dirLen 1,$dirLen) == "/" && substr($dir,0,1) !== "/")
                {
                    
    $dir "/" $dir "/"// add / to dir path on front and back
                
    }
                else
                {
                    
    /* correct value is there so do nothing */
                
    }
                
                if(
    $dirHandle)
                {
                    
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 5$rLen) == ".phar")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".phar")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($dir $files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($dir $files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($dir $files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($dir $files[$i] . ".phar");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($dir $files[$i] . ".phar");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($dir $files ".phar"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($dir $files ".phar");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($dir $files ".phar");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($dir $files ".phar");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($dir $files ".phar");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($dir $files ".phar");
                                break;
                            }
                        }
                    }
                }
                else if(
    $dir == "" || $dir == NULL)
                {
                    
    /* check if $files is * or ALL so we can include every .php file in that directory, if not and its an array include the specific files or if its not then include the single file */
                    
                    
    $dirHandle opendir(getcwd()); // current folder in use so get current working folder
                    
                    
    if($files == $fileField[0] || $files == $fileField[1] || $files == strtoupper($fileField[1]))
                    {
                        
    /* loop through folder */
                        
    while(($resources == readdir($dirHandle)) !== false)
                        {
                            
    /* do nothing if they are the . and .. files */
                            
    if($resources !== "." || $resources !== "..")
                            {
                                
    $rLen strlen($resources); // length of resource one each turn
                                
                                /* check if the end has .php extenstion */
                                
    if(substr($resources,$rLen 5$rLen) == ".phar")
                                {
                                    
    /* include files based on the include type which is universal for every file */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            include(
    $resources);
                                        break;
                                        
                                        case 
    "REQ":
                                            require(
    $resources);
                                        break;
                                        
                                        case 
    "INC_O":
                                            include_once(
    $resources);
                                        break;
                                        
                                        case 
    "REQ_O":
                                            require_once(
    $resources);
                                        break;
                                        
                                        default:
                                            include(
    $resources);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if(
    is_array($files))
                    {
                
                        
    /* get array size for loop and set count variable */
                        
    $aSize count($files); // array item count
                        
    $i// counter
                    
                        /* loop through the directory and include only the selected files */
                        
    while(($resources readdir($dirHandle)) !== false)
                        {
                            
    /* loop through files to see if they are and include file*/
                            
    for($i 0$i $aSize$i++)
                            {
                                
    /* check if the resources is a include file */
                                
    if($resources == $files[$i] . ".phar")
                                {
                                    
    /* do the include or require depending on the value of $type and set default to include */
                                    
    switch($type)
                                    {
                                        case 
    "INC":
                                            
    /* include files */
                                            
    include($files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "REQ":
                                            
    /* require files */
                                            
    require($files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "INC_O":
                                            
    /* include once files */
                                            
    include($files[$i] . ".phar");
                                        break;
                                        
                                        case 
    "REQ_O":
                                            
    /* require once files */
                                            
    require_once($files[$i] . ".phar");
                                        break;
                                    
                                        default:
                                            
    /* default is include */
                                            
    include($files[$i] . ".phar");
                                        break;
                                    }
                                
                                }
                            }
                        }
                    }
                    else
                    {
                        
    /* check if the files exists and if so include or require it, used on single files only */
                        
    if(file_exists($files ".phar"))
                        {
                            switch(
    $type)
                            {
                                case 
    "INC":
                                    
    /* include files */
                                    
    include($files ".phar");
                                break;
                                        
                                case 
    "REQ":
                                    
    /* require files */
                                    
    require($files ".phar");
                                break;
                                        
                                case 
    "INC_O":
                                    
    /* include once files */
                                    
    include($files ".phar");
                                break;
                                        
                                case 
    "REQ_O":
                                    
    /* require once files */
                                    
    require_once($files ".phar");
                                break;
                                    
                                default:
                                    
    /* default is include */
                                    
    include($files ".phar");
                                break;
                            }
                        }
                    }
                }
            
            }
        }
    ?>

    Sample Resources



    Include files: Make sure that they are all in the same directory or the sample will not work. It expects them to all be in the same folder.

    payment.php

    PHP:
    <?php
    /********************************************************\
    # function RentalPayment() calculates your payment for   #
    # your loan.                                             #
    # $Principle = total amount of money owed                #
    # $IntRate = interest rate one the loan                  #
    # $PayPerYear = Number of payments per year              #
    # $NumYears = Number of years you have to make payments  #
    \********************************************************/

    function RentalPayment($Principle$IntRate$PayPerYear$NumYears)
            {
                
    $numer$denom$b$e;
                
                
    $numer $IntRate $Principle $PayPerYear;

                
    $e = -($PayPerYear $NumYears);

                
    $b = ($IntRate $PayPerYear) + 1;

                
    $denom pow($b$e);

                return 
    $numer $denom;
            }
            
            function 
    hypotenuse($A$B)
            {
                if(
    is_numeric($A) && is_numeric($B))
                {
                
                    return 
    sqrt(($A $A) + ($B $B));
                }
                else
                {
                    return 
    0;
                }

            }
    ?>
    temp_conversions.php

    PHP:
    <?php

    $NaN 
    "NaN";

    /* Celsius to and from Fahrenheit conversions */

    function fahrenheitToCelsius($far,$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($far))
        {
            
            return 
    $NaN;
        }
        else
        {
        
            
    $CelsiusVal * ($far 32) / 9;
            
            if(
    $round == "true")
            
            {
            
                return 
    round($CelsiusVal);
                
            }
            else
            {
            
                return 
    $CelsiusVal;

            }
        
        }

    }

    function 
    celsiusToFahrenheit($cel$round="true")
    {

        global 
    $NaN;

        if(!
    is_numeric($cel))
        {
        
        return 
    $NaN;
        
        }
        else
        {
        
            
    $FahrenheitVal $cel 32;
            
            if(
    $round == "true")
            {
            
                return 
    round($FahrenheitVal);
            
            }
            else
            {

                return 
    $FahrenheitVal;
                
            }

        }
    }

    /* Kelvin to and from conversions */

    function celsiusToKelvin($cel$round="true")
    {

        global 
    $NaN;

        if(!
    is_numeric($cel))
        {

            return 
    $NaN;

        }
        else
        {

            
    $KelvinVal $cel 273;

            if(
    $round == "true")
            {

                return 
    round($KelvinVal);

            }
            else
            {

                return 
    $KelvinVal;

            }

        }

    }

    function 
    kelvinToCelsius($kel$round="true")
    {

        global 
    $NaN;
        
        if (!
    is_numeric($kel))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $CelsiusVal $kel 273;
            
            if (
    $round == "true")
            {
            
                return 
    round($CelsiusVal);
            
            }
            else
            {
            
                return 
    $CelsiusVal;
            
            }
        
        }

    }

    function 
    kelvinToFahrenheit($kel$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($kel))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $FahrenheitVal =  * ($kel 459) / 5;
            
            if(
    $round == "true")
            {
            
                return 
    round($FahrenheitVal);
            
            }
            else
            {
            
                return 
    $FahrenheitVal;
            
            }
        
        }

    }

    function 
    fahrenheitToKelvin($far$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($far))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $KelvinVal =  * ($far 459) / 9;
            
            if(
    $round == "true")
            {
            
                return 
    round($KelvinVal);
            
            }
            else
            {
            
                return 
    $KelvinVal;
            
            }
        
        }

    }

    function 
    kelvinToRankine($kel$round="true")
    {

        global 
    $NaN;
        
        if (!
    is_numeric($kel))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $RankineVal = ($kel 9) / 5;
            
            if(
    $round == "true")
            {
            
                return 
    round($RankineVal);
            
            }
            else
            {
            
                return 
    $RankineVal;
            
            }
        
        }

    }

    function 
    rankineToKelvin($ran$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($ran))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $KelvinVal = ($ran 5) / 9;
            
            if(
    $round == "true")
            {
            
                return 
    round($KelvinVal);
            
            }
            else
            {
            
                return 
    $KelvinVal;
            
            }
        
        }

    }

    /* To and From Rankine using Celsius and Fahrenheit because Rankine to Kelvin is defined in Kelvin functions  */

    function rankineToCelsius($ran$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($ran))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $CelsiusVal * ($ran 491) / 9;
            
            if(
    $round == "true")
            {
            
                return 
    round($CelsiusVal);
            
            }
            else
            {
            
                return 
    $CelsiusVal;
            
            }
        
        }

    }

    function 
    celsiusToRankine($cel$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($cel))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $RankineVal = ($cel 9) / 491;
            
            if(
    $round == "true")
            {
            
                return 
    round($RankineVal);
            
            }
            else
            {
            
                return 
    $RankineVal;
            
            }
        
        }

    }

    function 
    fahrenheitToRankine($far$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($far))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $RankineVal $far 459;
            
            if(
    $round == "true")
            {
            
                return 
    round($RankineVal);
            
            }
            else
            {
            
                return 
    $RankineVal;
            
            }
        
        }

    }

    function 
    rankineToFahrenheit($ran$round="true")
    {

        global 
    $NaN;
        
        if(!
    is_numeric($ran))
        {
        
            return 
    $NaN;
        
        }
        else
        {
        
            
    $FahrenheitVal $ran 459;
            
            if(
    $round == "true")
            {
            
                return 
    round($FahrenheitVal);
            
            }
            else
            {
            
                return 
    $FahrenheitVal;
            
            }
        
        }

    }

    ?>
    polygons.php

    PHP:
    <?php
    /* returns the circumference of a circle */
    function circumference($R,$inType "R")
    {

        
    $pi atan(1) * 4;

        
    $area;
        
        if(
    is_numeric($R))
        {
            
            switch(
    $inType)
            {
                
                case 
    "R":
                
                    
    $area = ($pi) * $R;
                
                    return 
    $area;
                    
                break;
                
                case 
    "D":
                
                    
    $area $pi $R;
                    
                    return 
    $area;
                    
                break;
                
                default:
                
                    return 
    0;
                    
                break;
                
            }
            
        }
        else
        {
        
            return 
    0;
            
        }
        
    }
    /* returns the radius from the circles area */
    function radiusFromArea($A,$roundPlace 0)
    {
        
        
    $pi atan(1) * 4;
        
        if(
    is_numeric($A))
        {
            
            
    $R sqrt($A $pi);
            
            if(
    is_numeric($roundPlace))
            {
            
                if(
    $roundPlace == 0)
                {
                
                    return 
    $R;
                
                }
                else
                {
                
                    return 
    round($R,$roundPlace);
                
                }
                
            }
            else
            {
            
                return 
    round($R);
                
            }
            
            
            
        }
        else
        {
            
            return 
    0;
        
        }
        
    }
    /* returns the radius of a circle from its circumference */
    function radiusFromCircumference($C,$roundPlace)
    {

        
    $pi atan(1) * 4;
        
        if(
    is_numeric($C))
        {
            
            
    $R $C / ($pi);
            
            if(
    is_numeric($roundPlace))
            {
                
                if(
    $roundPlace == 0)
                {
                    
                    return 
    $R;
                    
                }
                else
                {
                
                    return 
    round($R,$roundPlace);
                    
                }
                
            }
            else
            {
                
                return 
    round($R);
                
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
        
    }
    /* returns the volume of a sphere and takes in either radius or diameter */
    function sphereVol($R$round 0$inType "R")
    {
        
        if(
    is_numeric($R))
        {
        
            
    $pi atan(1) * 4;
            
    $frac 4/3;
            
            switch(
    $inType)
            {
                
                case 
    "R":
                
                
    $pre pow($R,3);
                
    $vol = ($frac $pi) * $pre;
                
                if(
    is_numeric($round))
                {
                    
                    if(
    $round == 0)
                    {
                        
                        return 
    $vol;
                        
                    }
                    else
                    {
                        
                        return 
    round($vol$round);
                        
                    }
                    
                }
                else
                {
                    
                    return 
    round($vol);
                    
                }
                        
                break;
                
                case 
    "D":
                
                
    $nR $R /2;
                
    $pre pow($nR3);
                
    $vol = ($frac $pi) * $pre;
                
                if(
    is_numeric($round))
                {
                    
                    if(
    $round == 0)
                    {
                        
                        return 
    $vol;
                        
                    }
                    else
                    {
                        
                        return 
    round($vol$round);
                        
                    }
                    
                }
                else
                {
                    
                    return 
    round($vol);
                    
                }
                break;
                
                default:
                
                return 
    0;
                
                break;
                
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
        
    }
    /*  returns the surface area of a sphere and takes in either radius or diameter  */
    function sphereSurfaceArea($R$round 0$inType "R")
    {

        
    $pi atan(1) * 4;
        
        
    $num 4;
        
        if(
    is_numeric($R))
        {
            
            switch(
    $inType)
            {
                
                case 
    "R":
                
                
    $pow pow($R,2);
                
                
    $area = ($num $pi) * $pow;
                
                if(
    is_numeric($round))
                {
                    
                    if(
    $round == 0)
                    {
                        
                        return 
    $area;
                        
                    }
                    else
                    {
                        
                        return 
    round($area$round);
                        
                    }
                    
                }
                else
                {
                    
                    return 
    round($area);
                    
                }
                break;
                
                case 
    "D":
                
                
    $nR $R /2;
                
                
    $pow pow($nR,2);
                
                
    $area = ($num $pi) * $pow;
                
                if(
    is_numeric($round))
                {
                    
                    if(
    $round == 0)
                    {
                        
                        return 
    $area;
                        
                    }
                    else
                    {
                        
                        return 
    round($area$round);
                        
                    }
                    
                }
                else
                {
                    
                    return 
    round($area);
                    
                }
                break;
                
                default:
                
                return 
    0;
                
                break;
                
                
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
        
    }
    /* returns the frustum volume for both cones and pyramids excepts characters for round param for regular rounding  */
    function frustumVol($h$b1$b2$round 0)
    {

        
    $frac 1/3;
        
        
    $sqrt sqrt($b1 $b2);
        
        if(
    is_numeric($h) && is_numeric($b1) && is_numeric($b2))
        {
            
            
    $vold $frac $h;
            
            
    $vol $vold * ($b1 $b2 $sqrt);
            
            if(
    is_numeric($round))
            {
                
                if(
    $round == 0)
                {
                    
                    return 
    $vol;
                    
                }
                else
                {
                    
                    return 
    round($vol$round);
                    
                }
                
            }
            else
            {
                
                return 
    round($vol);
                
            }
            
            
            
        }
        else
        {
            
            return 
    0;
            
        }
        
    }
    /* returns a pyramid frustum's area */
    function pyramidFrustumArea($s,$p1,$p2,$round=0)
    {

        
    $frac 1/2;
        
        if(
    is_numeric($s) && is_numeric($p1) && is_numeric($p2))
        {
            
            
    $P $p1 $p2;
            
            
    $area = ($frac $s) * $P;
            
            if(
    is_numeric($round))
            {
                
                if(
    $round == 0)
                {
                    
                    return 
    $area;
                    
                }
                else
                {
                    
                    return 
    round($area,$round);
                    
                }
                
            }
            else
            {
                
                return 
    round($area);
                
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
    }

    function 
    coneFrustumArea($s,$r1,$r2,$round=0)
    {
        
        
    $pi atan(1) * 4;
        
        if(
    is_numeric($s) && is_numeric($r1) && is_numeric($r2))
        {
            
            
    $R $r1 $r2;
            
    $area = ($pi $s) * $R;
            
            if(
    is_numeric($round))
            {
                
                if(
    $round == 0)
                {
                    
                    return 
    $area;
                    
                }
                else
                {
                    
                    return 
    round($area,$round);
                    
                }
                
            }
            else
            {
                
                return 
    round($area);
                
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
        
        
        
    }

    function 
    volume($b,$h,$poly)
    {

        
    $frac 1/3;
        
        
    $pi atan(1) * 4;

        if(
    is_numeric($h) && is_numeric($b))
        {
            
            switch(
    $poly)
            {
            
                case 
    "pyramid":
                
                    
    $vol $frac * ($b $h);
                    
                    return 
    $vol;
                
                break;
                
                case 
    "cone":
                
                    
    $R pow($b,2);
                    
    $vol = ($frac $pi) * ($R $h);
                    return 
    $vol;
                    
                break;
                
                case 
    "prism":
                
                    
    $vol $b $h;
                    
                    return 
    $vol;
                
                break;
                
                default:
                
                    return 
    0;
                    
                break;
            
            }
            
        }
        else
        {
            
            return 
    0;
            
        }
        
    }

    function 
    circleArea($r,$type="R")
    {
        
        
    $pi atan(1) * 4;
        
    $R $r/2;
        
        
        if(
    $type == "R")
        {
        
            
    $area $pi pow($r,2);
            return 
    $area;
        }
        else if(
    $type == "D")
        {
            
            
    $area = ($pi pow($r,2)) / 4;
            
            return 
    $area;
            
        }
        
    }

    ?>

    Sample Usage



    Sample script: importsamp.php

    PHP:
    <?php
    include("class_import.php");

    $importPackage = new import(); // new instance of the class(create an object from that class)
        
    /* include a single file from this files directory $dir is blank so it will use this files location. $files is not * or ALL and is not an array so include the single file. $type is set to INC so it includes the payment.php file if it exists. */

    $packs = array("payment""temp_conversions","polygons");

    $importPackage->package("",$packs,"INC"); 

    echo 
    "Your rental payment is " "$" round(RentalPayment(50000,0.75,375,5),2); // payment.php function

    echo " <br /> 98 &deg; is " fahrenheitToCelsius(98) . "&deg;'s Celsius<br />"// temp_conversions.php function

    echo "The circumference of a circle with a radius of 36 is: " circumference(36); // polygons.php function
    ?>

    References



    Java package system
     

    Attached Files:

    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice