Whats your style de codage?

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • Avatar de l’utilisateur
  • Inscription: Juil 25, 2005
  • Messages: 2735
  • Loc: Nashville, TN
  • Status: Offline

Message Septembre 28th, 2009, 11:22 am

Si vous êtes le type plein commentaire? Pas de commentaires? tout à onglets en place? Condensée que possible?

Quelles sont ces règles que vous ne pouvez pas vivre sans?

Quels sont ces bêtes noires qui vous haïssent juste de trouver dans le code de quelqu'un d'autre?

Im curieux de voir où le populus général Ozzu Falls :)
I'd love to change the world, but they won't give me the source code.
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Septembre 28th, 2009, 11:22 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Septembre 28th, 2009, 1:08 pm

J'aime accolades sur leurs propres lignes.
Code: [ Select ]
if(...)
{

}
  1. if(...)
  2. {
  3. }


I like! (ne pas) avoir des espaces de chaque côté.
Code: [ Select ]
if( ! something)
if(something)
  1. if( ! something)
  2. if(something)


Je n'aime pas les classes singleton qui vérifient les instances d'eux-mêmes au sein d'un constructeur.

Je n'aime pas basés dans l'espace d'indentation, j'aime onglets.

J'aime bien les arguments pour des choses comme printf d'être sur leurs propres lignes et en retrait. J'aime bien des observations avec l'argument numéro placé périodiquement.
Code: [ Select ]
printf(
    'This %2$s how %1$s like it',
    who,
    whether // %2$s
);
  1. printf(
  2.     'This %2$s how %1$s like it',
  3.     who,
  4.     whether // %2$s
  5. );


Je n'aime pas quand les numéros symboliques ne sont disponibles pour les fonctions de style printf et elles ne sont utilisées, peu importe la façon dont il existe peu d'arguments, ou si tous les arguments sont réutilisés dans le masque. Je ne me souviens pas combien de fois Ive a eu à revenir en arrière et retravailler un printf Lorsqu'un argument est ajoutée plus tard.

Je n'aime pas les commentaires qui ressemblent à de la documentation. Si le commentaire commence à prendre jusqu'à la moitié des éditeurs écran ce n'est pas un commentaire freakin, créez une page de documentation damn et remplacer whats dans le code avec un lien vers cette page déjà. Je ne veux pas entendre la merde pourtant de devoir accueillir une ou l'autre, vous n'avez pas besoin d'hébergement d'inclure un dossier Documents dans le paquet que vous pouvez lier à.

Je n'aime pas les multiples commentaires d'une ligne comme celle-ci quand des theres un multi-multi-syntaxe réelle commentaire disponible en ligne.
Code: [ Select ]
// one
// two
// three
  1. // one
  2. // two
  3. // three


Je n'aime pas les commentaires qui ressemblent à de la documentation. Oui, cela est utile de mentionner deux fois.

Je n'aime pas l'espace entre la clôture pare nothesis. Je peux traiter avec des espaces après l'appel, la première fonction.
Code: [ Select ]
if(something( something2( something2())))


J'aime bien les guillemets quand il le faut.
Code: [ Select ]
$var = "My name is $name";
$var = 'My name is ' . $name;
  1. $var = "My name is $name";
  2. $var = 'My name is ' . $name;


I like this
Code: [ Select ]
$var = "My name is {$obj->name}";


Je n'aime pas cette
Code: [ Select ]
$var = "My name is $obj->name";


Mais je n'aime ce
Code: [ Select ]
$var = "My name is $name";


Thats une question de comment fonctionne mon syntaxe surligneur.

Thats tout ce qui vient à l'esprit pour le moment.
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Septembre 28th, 2009, 8:59 pm

Il serait peut-être plus facile si je vous montrer un exemple de mon morceau de codage et de vous montrer comment je fais:
PHP Code: [ Select ]
<?php
/*
 * install.php
 *      A global installation class created to be used with any kind of application.
 *
 * Before doing anything with this class, I recommend you read the documentation to figure out
 *  what you are doing. That's so you won't accidentally mess something up.
 */
 
class install {
 
    /*
     * The stages that the script would have in it's overall installation process.
     *      This is to ensure that the user wouldn't just put anything random into the
     *      url and try to do something. Also for automation.
     */
   
    var $stages = array('stage_1','stage_2','stage_3');
   
    /*
     * This determines if we would have anything to do with SQL
     */
   
    var $use_sql = true;
   
    /*
     * This would store the SQL information once/if recieved (For the stage submitted on... later on, uses $_SESSION)
     */
   
    var $sql = array('HOST'  => null,       // MySQL Host
                     'USER'  => null,       // MySQL Username
                     'PASS'  => null,       // MySQL Passowrd
                     'DBASE' => null,       // MySQL Database Name
                     'TBL_PRE' => null);    // MySQL Table Prefix
 
    /*
     * Determines if a global file would be created at the end.
     *
     * The global file would consist of the MySQL host, username, password and the database name
     */
   
    var $create_global = true;
   
    /*
     * Determines the location that the configuration file would be saved to (Make sure it exists)
     */
   
    var $gl_loc = 'includes/';
   
    /*
     * Determines the name for the global file
     */
   
    var $globals = 'global';
   
    /*
     * Determines the global file type (also used as the extension for the global file)
     *
     *  Possible types:
     *      XML, PHP
     *
     * NOTE: If you know PHP it would be easy for you to add more types here
     */
   
    var $global_type = 'XML';
   
    /*
     * Set to true if you are installing and want to check if the application is already installed.
     *
     * If the global file exists, than the application is installed. Otherwise the application is not installed.
     */
   
    var $check_installed = true;
   
    /*
     * location where the forms are located (Set the filename equal to their stage)
     */
   
    var $forms = 'forms/';
   
    /*
     * Location of the SQL file holding the SQL queries to be made
     *
     * NOTE:
     *      Make sure that every CREATE query is set into an array called $create even if there is only one query.
     *      Make sure that every INSERT query is set into an array called $insert even if there is only one query.
     */
   
    var $sqls = 'sql/queries.php';
   
    /*
     * The following forces the class to die on error and print the error
     */
   
    var $force_errors = false;
   
    /*
     * Set the following true if you want the script to debug at each error
     */
   
    var $debug = true;
   
    /*
     * Determines weather you want a HEADER redirect or one by JavaScript
     *
     *  Possible values:
     *      HEADER (PHP Header redirection)
     *      JavaScript (Javascript Window redirection)
     */
   
    var $redirect_type = 'JavaScript';
   
    /*
     * Redirection place where users would go to upon finishing installation
     */
   
    var $redirect_loc = 'documentation.html';
   
    /*
     * The following variables are variables for this class. Configure them at your own risk
     */
   
    var $error = array();               // Silent errors returned by this class
    var $errno = 0;                     // Number of errors generated by the class
    var $global_loc = null;             // The location of the global file created
   
    /*
     * function __construct( VOID )
     *
     * Function that basically sets certain variables.
     */
   
    function __construct()
    {
        // Creating the location where the global file would be and it's name with extension type
        $this->global_loc = $this->gl_loc . $this->globals . '.' . strtolower($this->global_type);
    }
   
    /*
     * function setup( [ string $stage ] )
     *      @string $stage - The current stage of the installation. (Includes the form)
     *
     * Includes the form for the current setup stage
     */
   
    function setup()
    {
        // Making sure we've got the right stage
        $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
       
        // Including the corrent stage's form
        include_once($this->forms . $stage . '.php');
    }
   
    /*
     * function cont( VOID )
     *
     * Checks the current stage and determines what the next stage should be
     */
   
    function cont()
    {
        // Getting the current stage the user is on
        $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
       
        // Advancing to the next stage
        $stage = $this->advance($stage);
       
        // Checking if the installation process was finished
        if($stage == sizeof($this->stages) || $stage == false)
        {
            // Checking if we need to create the global file
            if($this->create_global == true)
            {
                $this->create_global();
            }
            else
            {
                /*
                 * The redirect type here is set to JavaScript to over-ride HEADER.
                 *  Reason being, is when the installation is done and the script
                 *  is supposed to redirect the user to the page you specified,
                 *  then the HEADER tries to look for a class that isn't there,
                 *  but JavaScript does it correctly. Don't know why.
                 */
                $this->redirect_type = "JavaScript";
               
                // Redirecting the user to the page you specified
                $this->redirect($this->redirect_loc);
            }
            break;
        }
       
        // Getting the page the user is on (The installation page)
        $page = $_SERVER['PHP_SELF'];
       
        // Redirecting the user to the next stage
        $this->redirect("{$page}?stage={$this->stages[$stage]}");
    }
   
    /*
     * function advance( string $stage )
     *      @string $stage - Current stage that the function would determine the next from
     *
     * Redirects the user to the next stage after one is done
     */
   
    function advance($stage)
    {
        // Going through the stages array and looking for the current stage
        foreach($this->stages as $id => $st)
        {
            // Adding 1 to the current stage key
            if($stage == $st)
            {
                return $id + 1;
            }
        }
       
        // Getting the amount of stages there are
        $stage_size = sizeof($this->stages);
       
        // Checking if this is the last stage or if the stage doesn't exist
        if($id == $stage_size)
        {
            return false;
        }
        else
        {
            // Neither found... we don't need to advance since the installation is done!
            $this->error('STAGE_ADVANCE', "An unknown error occured in " . (__FILE__) . ' - (<strong>' . (__LINE__) . '</strong>)');
           
            return $id;
        }
    }
   
    /*
     * function set_sql( string $host, string $user, string $pass, string $dbname, string $tbl_pre )
     *      @string $host - The MySQL Host
     *      @string $user - The MySQL User
     *      @string $pass - The MySQL Password
     *      @string $dbname - The Database Name
     *      @string $tbl_pre - The MySQL Table Prefix
     *
     * Function that sets the class sql array to their appropriate values
     */
   
    function set_sql($host, $user, $pass, $dbname, $tbl_pre)
    {
        // Setting the SQL array to it's appropriate values
        $this->sql = array('HOST'  => $host,
                           'USER'  => $user,
                           'PASS'  => $pass,
                           'DBASE' => $dbname,
                           'TBL_PRE' => $tbl_pre);
       
        // Setting the SQL session
        $_SESSION['sql'] = $this->sql;
       
        // Checking if the values submitted were valid
        if($this->check_sql($host, $user, $pass, $dbname) == true)
        {
            // Values were correct
            return true;
        }
       
        // Values were incorrect
        $this->error('SETTING_SQL', "Values submitted weren't valid");
        return false;
    }
   
    /*
     * function create_global( VOID )
     *
     * Function that creates the global file.
     */
   
    function create_global()
    {
        // Checking if we need to check if the application is already installed
        if($this->check_installed == true)
        {
            // Checking if the global file exists
            if(file_exists($this->global_loc) == true)
            {
                // Killing script execution with an error
                $this->error('APP_INSTALLATION', "Application already installed.");
            }
        }
       
        // Checking what kind of global file it would be... PHP or XML
        switch($this->global_type)
        {
            case 'XML':
                // The XML content
                $content = <<<EOT
<?xml version="1.0" encoding="ISO-8859-1"?>
<database>
    <host>{$_SESSION['sql']['HOST']}</host>
    <user>{$_SESSION['sql']['USER']}</user>
    <pass>{$_SESSION['sql']['PASS']}</pass>
    <dbname>{$_SESSION['sql']['DBASE']}</dbname>
    <table_prefix>{$_SESSION['sql']['TBL_PRE']}</table_prefix>
</database>
EOT;
               
                break;
            case 'PHP':
                // The PHP content
                $content = <<<EOT
<?php
 
/* The global configuration file */
 
// The MySQL Host
$host = "{$_SESSION['sql']['HOST']}";
 
// The MySQL User
$user = "{$_SESSION['sql']['USER']}";
 
// The MySQL password
$pass = "{$_SESSION['sql']['PASS']}";
 
// The MySQL Database Name
$dbname = "{$_SESSION['sql']['DBASE']}";
 
// The MySQL Table Prefix
$tbl_pre = "{$_SESSION['sql']['TBL_PRE']}";
 
?>
EOT;
                break;
            default:
                // Neither of the correct options were given. Defaulting to XML
                $this->global_type = 'XML';
                $this->create_global();
                break;
        }
       
        // Creating the Global File
        if(touch($this->global_loc) != false)
        {
            // Putting the contents into the global file
            if(file_put_contents($this->global_loc, $content) != false)
            {
                // Checking if we are to create SQL tables into the created database
                if($this->use_sql == true)
                {
                    // Doing the SQL portion of the installation
                    $this->do_sql();
                }
                else
                {
                    // Redirecting the user to the finish page
                    $this->redirect($this->redirect_loc);
                }
            }
        }
        else
        {
            // The contents weren't put into the global file. Set the error and return false
            $this->error('GLOBAL_FILE_CONTENT', "Contents were failed to be put into the global file  {$this->global_loc}");
            return false;
        }
    }
   
    /*
     * function do_sql( VOID )
     *
     * Function that carries out the available SQL queries provided in the SQL file
     */
   
    function do_sql()
    {
        // Requiring the SQLs page
        require $this->sqls;
       
        // Connecting to MySQL
        $link = mysql_connect($_SESSION['sql']['HOST'], $_SESSION['sql']['USER'], $_SESSION['sql']['PASS']) or die(mysql_error());
       
        // Selecting the database
        $db = mysql_select_db($_SESSION['sql']['DBASE']) or die(mysql_error());
       
        // Checking if CREATE array exists
        if(isset($create))
        {
            // Doing the SQL table creation
            foreach($create as $query)
            {
                // executing the create queries
                $result = mysql_query($query, $link);
               
                // Checking of one of the queries failed
                if($result == false)
                {
                    $this->error('SQL_QUERY_CREATE',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
                    return false;
                    break;
                }
            }
        }
       
        // Checking if INSERT array exists
        if(isset($insert))
        {
            // Looping through each insert query and doing them
            foreach($insert as $query)
            {
                // Executing the queries
                $result = mysql_query($query, $link);
               
                // Checking if one of them failed.
                if($result == false)
                {
                    $this->error('SQL_QUERY_INSERT',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
                    return false;
                    break;
                }
            }
        }
       
        // Making sure there were no possible errors
        if(!isset($create) && !isset($insert))
        {
            $this->error('DO_SQL_EXECUTION',"The SQL file provided doesn't have the required arrays set.");
            return false;
        }
       
        // Everything carried out correctly. Redirect to the homepage
        $this->redirect($this->redirect_loc);
    }
   
    function redirect($page)
    {
        // Redirecting the user to the next stage based on the preferable redirection type
        switch($this->redirect_type)
        {
            case "HEADER":
                // It's header... turning the output buffering on
                ob_start();
               
                // Redirecting the user
                header("LOCATION: {$page}");
               
                // Sending the content and turning the output buffer off
                ob_end_flush();
                break;
            case "JavaScript":
                // Redirecting the user with JavaScript
                echo "<script language=\"JavaScript\">window.location=\"{$page}\";</script>";
                break;
            default:
                // Not one possible redirection type is given... using JavaScript by default
                $this->redirect_type = 'JavaScript';
                $this->redirect($page);
                break;
        }
    }
   
    /*
     * function error( string $key, string $msg )
     *      @string $key - The key to the error array
     *      @string $msg - The message to the error array associated to the given $key
     *
     * Sets the error array and checks on farther error/debugging options
     */
   
    function error($key, $msg)
    {
        // Setting the error array
        $this->error[$key] = $msg;
       
        // Incrementing the error number value
        $this->errno += 1;
       
        // Checking if we should debug the error(s)
        if($this->debug == true)
        {
            echo $this->debug();
           
            // Checking if we have to force this error
            if($this->force_errors == true)
            {
                exit;
            }
        }
       
        // Checking if we should force the error to be shown
        if($this->force_errors == true)
        {
            die("<strong>$key</strong> $msg");
        }
    }
   
    /*
     * function debug( VOID )
     *
     * Function that does some debugging options on produced/generated errors
     */
   
    function debug()
    {
        // Going through each error recieved and generating the rest of the debugging text
        $err_name = array_keys($this->error);
        $err_msg = $this->retaval($this->error, $this->errno - 1, false, true);
       
        $text = "<p><strong>{$err_name[$this->errno - 1]}</strong>: $err_msg<br />\n";
       
        // Finishing off the debugging text
        $text .= "</p>";
       
        // Returning the generated text
        return $text;
    }
   
    /************************************************************************************************
     * The following functions are a functions repository                                           *
     *  To what you may need for the installation                                                   *
     *  process.                                                                                    *
     ************************************************************************************************/
     
    /*
     * function check_sql( [ string $db ] )
     *      @string $db - Whether or not we should check the validicy of the database
     *
     * Checks if the MySQL credentials are correct
     */
   
    function check_sql($db = true)
    {
        // Connecting to MySQL and capturing the resource handler
        $link = @mysql_connect($this->sql['HOST'], $this->sql['USER'], $this->sql['PASS']);
       
        // Checking if we are not connected
        if($link == false)
        {
            $this->error('SQL_CONNECTION', mysql_errno() . ' - ' . mysql_error());
        }
       
        // Checking if we should select the database
        if($db == true)
        {
            // Selecting the database and capturing the resource handler
            $db = @mysql_select_db($this->sql['DBASE']);
           
            // Checking if there were any errors in the connection
            if($db == false)
            {
                $this->error('SQL_DB_SELECT', mysql_errno() . ' - ' . mysql_error());
            }
        }
       
        // Closing the MySQL Connection if we are connected
        if($link != false)
        {
            @mysql_close($link);
        }
       
        // Returning the result.
        return (($link != false || $db != false) ? true : false);
    }
   
    /*
     * function get_xml( string $what )
     *      @string $what - The variable to take from an XML File
     *
     * Retrieves a variable from the global file
     */
   
    function get_xml($what)
    {
        // Loading the global XML file
        $db = (array) simplexml_load_file($this->global_file);
       
        // Returing the requested variable to the user
        return (string) $db[$what];
    }
   
    /*
     * function add( string $value [, mix $key] )
     *      @string $value - The value to add to the additional variable
     *      @mix $key - This variable could be set to the following possible values
     *          * 00 (2 zeros) - If you want a numeric, automatically-set keys
     *          * Anything else - If the key is not set to 00, then the key would be what you put it. (string value and key only)
     *
     * Creates an additional array for whatever needs you may need.
     */
   
    function add($value, $key = 00)
    {
        // Checking if the key should be numeric
        if($key == 00)
        {
            // Getting the next available numeric key so we wouldn't over ride a value
            foreach($this->add as $id => $val)
            {
                // Checking if the key is numeric
                if(is_numeric($id))
                {
                    $keyed = $id;
                }
            }
           
            // Incrementing the last numeric key by one
            $key = $keyed + 1;
           
            // Unsetting the useless $val
            unset($val);
        }
       
        // Generating the session
        $_SESSION['ADD'][$key] = $value;
       
        // Returning true
        return true;
    }
   
    /*
     * function retaval( array $var, int $akey [, boolean $array [, boolean $value [, boolean $numeric]]])
     *      @array $var - The array to retrieve the value/key from
     *      @integer $akey - The key/value to retrieve in numeric terms (1, 2...)
     *      @boolean $array - Determines if the result should come as an array with the key and the
                                value.
     *      @boolean $value - Set true if you want the value, set false if you want the key...
     *                          set to null if you want an array returned with the key and value
     *      @boolean $numeric - Set to true if you want the key to be numeric... otherwise set
     *                              to false if you want the key to be what it previously was
     *
     * A function to retrieve a value/key from an array. Returns a string by default, or an
     *  array if you need the key to be numeric
     *  
     */
   
    function retaval($var, $akey, $array = false,  $value = false, $numeric = false)
    {
        // Setting some variables
        $i = 0;
        $num = count($var);
       
        // Looping though the array and choosing the key/value that was asked for
        foreach($var as $key => $val)
        {
            // Setting the return value to the current key/value
            if($array == false)
            {
                $return = (($value == false) ? $key : $val);
            }
           
            // Checking if we need the result as an array or a string
            if($array != false)
            {
                // Setting the key to be whatever it needs to be
                $key = (($numeric == false) ? $key : 0);
               
                // Setting the return array to be whatever it was meant to be
                $return[$key] = (($value == false) ? ((is_null($value)) ? $value : $key) : $val);
            }
           
            // Making sure we stop when we have the correct key/value
            if($i == $akey)
            {
                break;
            }
           
            // Incrementing the counter
            ++$i;
        }
       
        //  Returning the return value if there is any
        return (($i > $num) ? false : $return);
    }
}
?>
  1. <?php
  2. /*
  3.  * install.php
  4.  *      A global installation class created to be used with any kind of application.
  5.  *
  6.  * Before doing anything with this class, I recommend you read the documentation to figure out
  7.  *  what you are doing. That's so you won't accidentally mess something up.
  8.  */
  9.  
  10. class install {
  11.  
  12.     /*
  13.      * The stages that the script would have in it's overall installation process.
  14.      *      This is to ensure that the user wouldn't just put anything random into the
  15.      *      url and try to do something. Also for automation.
  16.      */
  17.    
  18.     var $stages = array('stage_1','stage_2','stage_3');
  19.    
  20.     /*
  21.      * This determines if we would have anything to do with SQL
  22.      */
  23.    
  24.     var $use_sql = true;
  25.    
  26.     /*
  27.      * This would store the SQL information once/if recieved (For the stage submitted on... later on, uses $_SESSION)
  28.      */
  29.    
  30.     var $sql = array('HOST'  => null,       // MySQL Host
  31.                      'USER'  => null,       // MySQL Username
  32.                      'PASS'  => null,       // MySQL Passowrd
  33.                      'DBASE' => null,       // MySQL Database Name
  34.                      'TBL_PRE' => null);    // MySQL Table Prefix
  35.  
  36.     /*
  37.      * Determines if a global file would be created at the end.
  38.      *
  39.      * The global file would consist of the MySQL host, username, password and the database name
  40.      */
  41.    
  42.     var $create_global = true;
  43.    
  44.     /*
  45.      * Determines the location that the configuration file would be saved to (Make sure it exists)
  46.      */
  47.    
  48.     var $gl_loc = 'includes/';
  49.    
  50.     /*
  51.      * Determines the name for the global file
  52.      */
  53.    
  54.     var $globals = 'global';
  55.    
  56.     /*
  57.      * Determines the global file type (also used as the extension for the global file)
  58.      *
  59.      *  Possible types:
  60.      *      XML, PHP
  61.      *
  62.      * NOTE: If you know PHP it would be easy for you to add more types here
  63.      */
  64.    
  65.     var $global_type = 'XML';
  66.    
  67.     /*
  68.      * Set to true if you are installing and want to check if the application is already installed.
  69.      *
  70.      * If the global file exists, than the application is installed. Otherwise the application is not installed.
  71.      */
  72.    
  73.     var $check_installed = true;
  74.    
  75.     /*
  76.      * location where the forms are located (Set the filename equal to their stage)
  77.      */
  78.    
  79.     var $forms = 'forms/';
  80.    
  81.     /*
  82.      * Location of the SQL file holding the SQL queries to be made
  83.      *
  84.      * NOTE:
  85.      *      Make sure that every CREATE query is set into an array called $create even if there is only one query.
  86.      *      Make sure that every INSERT query is set into an array called $insert even if there is only one query.
  87.      */
  88.    
  89.     var $sqls = 'sql/queries.php';
  90.    
  91.     /*
  92.      * The following forces the class to die on error and print the error
  93.      */
  94.    
  95.     var $force_errors = false;
  96.    
  97.     /*
  98.      * Set the following true if you want the script to debug at each error
  99.      */
  100.    
  101.     var $debug = true;
  102.    
  103.     /*
  104.      * Determines weather you want a HEADER redirect or one by JavaScript
  105.      *
  106.      *  Possible values:
  107.      *      HEADER (PHP Header redirection)
  108.      *      JavaScript (Javascript Window redirection)
  109.      */
  110.    
  111.     var $redirect_type = 'JavaScript';
  112.    
  113.     /*
  114.      * Redirection place where users would go to upon finishing installation
  115.      */
  116.    
  117.     var $redirect_loc = 'documentation.html';
  118.    
  119.     /*
  120.      * The following variables are variables for this class. Configure them at your own risk
  121.      */
  122.    
  123.     var $error = array();               // Silent errors returned by this class
  124.     var $errno = 0;                     // Number of errors generated by the class
  125.     var $global_loc = null;             // The location of the global file created
  126.    
  127.     /*
  128.      * function __construct( VOID )
  129.      *
  130.      * Function that basically sets certain variables.
  131.      */
  132.    
  133.     function __construct()
  134.     {
  135.         // Creating the location where the global file would be and it's name with extension type
  136.         $this->global_loc = $this->gl_loc . $this->globals . '.' . strtolower($this->global_type);
  137.     }
  138.    
  139.     /*
  140.      * function setup( [ string $stage ] )
  141.      *      @string $stage - The current stage of the installation. (Includes the form)
  142.      *
  143.      * Includes the form for the current setup stage
  144.      */
  145.    
  146.     function setup()
  147.     {
  148.         // Making sure we've got the right stage
  149.         $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
  150.        
  151.         // Including the corrent stage's form
  152.         include_once($this->forms . $stage . '.php');
  153.     }
  154.    
  155.     /*
  156.      * function cont( VOID )
  157.      *
  158.      * Checks the current stage and determines what the next stage should be
  159.      */
  160.    
  161.     function cont()
  162.     {
  163.         // Getting the current stage the user is on
  164.         $stage = (!isset($_GET['stage'])) ? $this->stages[0] : $_GET['stage'];
  165.        
  166.         // Advancing to the next stage
  167.         $stage = $this->advance($stage);
  168.        
  169.         // Checking if the installation process was finished
  170.         if($stage == sizeof($this->stages) || $stage == false)
  171.         {
  172.             // Checking if we need to create the global file
  173.             if($this->create_global == true)
  174.             {
  175.                 $this->create_global();
  176.             }
  177.             else
  178.             {
  179.                 /*
  180.                  * The redirect type here is set to JavaScript to over-ride HEADER.
  181.                  *  Reason being, is when the installation is done and the script
  182.                  *  is supposed to redirect the user to the page you specified,
  183.                  *  then the HEADER tries to look for a class that isn't there,
  184.                  *  but JavaScript does it correctly. Don't know why.
  185.                  */
  186.                 $this->redirect_type = "JavaScript";
  187.                
  188.                 // Redirecting the user to the page you specified
  189.                 $this->redirect($this->redirect_loc);
  190.             }
  191.             break;
  192.         }
  193.        
  194.         // Getting the page the user is on (The installation page)
  195.         $page = $_SERVER['PHP_SELF'];
  196.        
  197.         // Redirecting the user to the next stage
  198.         $this->redirect("{$page}?stage={$this->stages[$stage]}");
  199.     }
  200.    
  201.     /*
  202.      * function advance( string $stage )
  203.      *      @string $stage - Current stage that the function would determine the next from
  204.      *
  205.      * Redirects the user to the next stage after one is done
  206.      */
  207.    
  208.     function advance($stage)
  209.     {
  210.         // Going through the stages array and looking for the current stage
  211.         foreach($this->stages as $id => $st)
  212.         {
  213.             // Adding 1 to the current stage key
  214.             if($stage == $st)
  215.             {
  216.                 return $id + 1;
  217.             }
  218.         }
  219.        
  220.         // Getting the amount of stages there are
  221.         $stage_size = sizeof($this->stages);
  222.        
  223.         // Checking if this is the last stage or if the stage doesn't exist
  224.         if($id == $stage_size)
  225.         {
  226.             return false;
  227.         }
  228.         else
  229.         {
  230.             // Neither found... we don't need to advance since the installation is done!
  231.             $this->error('STAGE_ADVANCE', "An unknown error occured in " . (__FILE__) . ' - (<strong>' . (__LINE__) . '</strong>)');
  232.            
  233.             return $id;
  234.         }
  235.     }
  236.    
  237.     /*
  238.      * function set_sql( string $host, string $user, string $pass, string $dbname, string $tbl_pre )
  239.      *      @string $host - The MySQL Host
  240.      *      @string $user - The MySQL User
  241.      *      @string $pass - The MySQL Password
  242.      *      @string $dbname - The Database Name
  243.      *      @string $tbl_pre - The MySQL Table Prefix
  244.      *
  245.      * Function that sets the class sql array to their appropriate values
  246.      */
  247.    
  248.     function set_sql($host, $user, $pass, $dbname, $tbl_pre)
  249.     {
  250.         // Setting the SQL array to it's appropriate values
  251.         $this->sql = array('HOST'  => $host,
  252.                            'USER'  => $user,
  253.                            'PASS'  => $pass,
  254.                            'DBASE' => $dbname,
  255.                            'TBL_PRE' => $tbl_pre);
  256.        
  257.         // Setting the SQL session
  258.         $_SESSION['sql'] = $this->sql;
  259.        
  260.         // Checking if the values submitted were valid
  261.         if($this->check_sql($host, $user, $pass, $dbname) == true)
  262.         {
  263.             // Values were correct
  264.             return true;
  265.         }
  266.        
  267.         // Values were incorrect
  268.         $this->error('SETTING_SQL', "Values submitted weren't valid");
  269.         return false;
  270.     }
  271.    
  272.     /*
  273.      * function create_global( VOID )
  274.      *
  275.      * Function that creates the global file.
  276.      */
  277.    
  278.     function create_global()
  279.     {
  280.         // Checking if we need to check if the application is already installed
  281.         if($this->check_installed == true)
  282.         {
  283.             // Checking if the global file exists
  284.             if(file_exists($this->global_loc) == true)
  285.             {
  286.                 // Killing script execution with an error
  287.                 $this->error('APP_INSTALLATION', "Application already installed.");
  288.             }
  289.         }
  290.        
  291.         // Checking what kind of global file it would be... PHP or XML
  292.         switch($this->global_type)
  293.         {
  294.             case 'XML':
  295.                 // The XML content
  296.                 $content = <<<EOT
  297. <?xml version="1.0" encoding="ISO-8859-1"?>
  298. <database>
  299.     <host>{$_SESSION['sql']['HOST']}</host>
  300.     <user>{$_SESSION['sql']['USER']}</user>
  301.     <pass>{$_SESSION['sql']['PASS']}</pass>
  302.     <dbname>{$_SESSION['sql']['DBASE']}</dbname>
  303.     <table_prefix>{$_SESSION['sql']['TBL_PRE']}</table_prefix>
  304. </database>
  305. EOT;
  306.                
  307.                 break;
  308.             case 'PHP':
  309.                 // The PHP content
  310.                 $content = <<<EOT
  311. <?php
  312.  
  313. /* The global configuration file */
  314.  
  315. // The MySQL Host
  316. $host = "{$_SESSION['sql']['HOST']}";
  317.  
  318. // The MySQL User
  319. $user = "{$_SESSION['sql']['USER']}";
  320.  
  321. // The MySQL password
  322. $pass = "{$_SESSION['sql']['PASS']}";
  323.  
  324. // The MySQL Database Name
  325. $dbname = "{$_SESSION['sql']['DBASE']}";
  326.  
  327. // The MySQL Table Prefix
  328. $tbl_pre = "{$_SESSION['sql']['TBL_PRE']}";
  329.  
  330. ?>
  331. EOT;
  332.                 break;
  333.             default:
  334.                 // Neither of the correct options were given. Defaulting to XML
  335.                 $this->global_type = 'XML';
  336.                 $this->create_global();
  337.                 break;
  338.         }
  339.        
  340.         // Creating the Global File
  341.         if(touch($this->global_loc) != false)
  342.         {
  343.             // Putting the contents into the global file
  344.             if(file_put_contents($this->global_loc, $content) != false)
  345.             {
  346.                 // Checking if we are to create SQL tables into the created database
  347.                 if($this->use_sql == true)
  348.                 {
  349.                     // Doing the SQL portion of the installation
  350.                     $this->do_sql();
  351.                 }
  352.                 else
  353.                 {
  354.                     // Redirecting the user to the finish page
  355.                     $this->redirect($this->redirect_loc);
  356.                 }
  357.             }
  358.         }
  359.         else
  360.         {
  361.             // The contents weren't put into the global file. Set the error and return false
  362.             $this->error('GLOBAL_FILE_CONTENT', "Contents were failed to be put into the global file  {$this->global_loc}");
  363.             return false;
  364.         }
  365.     }
  366.    
  367.     /*
  368.      * function do_sql( VOID )
  369.      *
  370.      * Function that carries out the available SQL queries provided in the SQL file
  371.      */
  372.    
  373.     function do_sql()
  374.     {
  375.         // Requiring the SQLs page
  376.         require $this->sqls;
  377.        
  378.         // Connecting to MySQL
  379.         $link = mysql_connect($_SESSION['sql']['HOST'], $_SESSION['sql']['USER'], $_SESSION['sql']['PASS']) or die(mysql_error());
  380.        
  381.         // Selecting the database
  382.         $db = mysql_select_db($_SESSION['sql']['DBASE']) or die(mysql_error());
  383.        
  384.         // Checking if CREATE array exists
  385.         if(isset($create))
  386.         {
  387.             // Doing the SQL table creation
  388.             foreach($create as $query)
  389.             {
  390.                 // executing the create queries
  391.                 $result = mysql_query($query, $link);
  392.                
  393.                 // Checking of one of the queries failed
  394.                 if($result == false)
  395.                 {
  396.                     $this->error('SQL_QUERY_CREATE',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
  397.                     return false;
  398.                     break;
  399.                 }
  400.             }
  401.         }
  402.        
  403.         // Checking if INSERT array exists
  404.         if(isset($insert))
  405.         {
  406.             // Looping through each insert query and doing them
  407.             foreach($insert as $query)
  408.             {
  409.                 // Executing the queries
  410.                 $result = mysql_query($query, $link);
  411.                
  412.                 // Checking if one of them failed.
  413.                 if($result == false)
  414.                 {
  415.                     $this->error('SQL_QUERY_INSERT',"The SQL query ({$query}) failed to execute: MySQL Returned '" . mysql_error() . "'");
  416.                     return false;
  417.                     break;
  418.                 }
  419.             }
  420.         }
  421.        
  422.         // Making sure there were no possible errors
  423.         if(!isset($create) && !isset($insert))
  424.         {
  425.             $this->error('DO_SQL_EXECUTION',"The SQL file provided doesn't have the required arrays set.");
  426.             return false;
  427.         }
  428.        
  429.         // Everything carried out correctly. Redirect to the homepage
  430.         $this->redirect($this->redirect_loc);
  431.     }
  432.    
  433.     function redirect($page)
  434.     {
  435.         // Redirecting the user to the next stage based on the preferable redirection type
  436.         switch($this->redirect_type)
  437.         {
  438.             case "HEADER":
  439.                 // It's header... turning the output buffering on
  440.                 ob_start();
  441.                
  442.                 // Redirecting the user
  443.                 header("LOCATION: {$page}");
  444.                
  445.                 // Sending the content and turning the output buffer off
  446.                 ob_end_flush();
  447.                 break;
  448.             case "JavaScript":
  449.                 // Redirecting the user with JavaScript
  450.                 echo "<script language=\"JavaScript\">window.location=\"{$page}\";</script>";
  451.                 break;
  452.             default:
  453.                 // Not one possible redirection type is given... using JavaScript by default
  454.                 $this->redirect_type = 'JavaScript';
  455.                 $this->redirect($page);
  456.                 break;
  457.         }
  458.     }
  459.    
  460.     /*
  461.      * function error( string $key, string $msg )
  462.      *      @string $key - The key to the error array
  463.      *      @string $msg - The message to the error array associated to the given $key
  464.      *
  465.      * Sets the error array and checks on farther error/debugging options
  466.      */
  467.    
  468.     function error($key, $msg)
  469.     {
  470.         // Setting the error array
  471.         $this->error[$key] = $msg;
  472.        
  473.         // Incrementing the error number value
  474.         $this->errno += 1;
  475.        
  476.         // Checking if we should debug the error(s)
  477.         if($this->debug == true)
  478.         {
  479.             echo $this->debug();
  480.            
  481.             // Checking if we have to force this error
  482.             if($this->force_errors == true)
  483.             {
  484.                 exit;
  485.             }
  486.         }
  487.        
  488.         // Checking if we should force the error to be shown
  489.         if($this->force_errors == true)
  490.         {
  491.             die("<strong>$key</strong> $msg");
  492.         }
  493.     }
  494.    
  495.     /*
  496.      * function debug( VOID )
  497.      *
  498.      * Function that does some debugging options on produced/generated errors
  499.      */
  500.    
  501.     function debug()
  502.     {
  503.         // Going through each error recieved and generating the rest of the debugging text
  504.         $err_name = array_keys($this->error);
  505.         $err_msg = $this->retaval($this->error, $this->errno - 1, false, true);
  506.        
  507.         $text = "<p><strong>{$err_name[$this->errno - 1]}</strong>: $err_msg<br />\n";
  508.        
  509.         // Finishing off the debugging text
  510.         $text .= "</p>";
  511.        
  512.         // Returning the generated text
  513.         return $text;
  514.     }
  515.    
  516.     /************************************************************************************************
  517.      * The following functions are a functions repository                                           *
  518.      *  To what you may need for the installation                                                   *
  519.      *  process.                                                                                    *
  520.      ************************************************************************************************/
  521.      
  522.     /*
  523.      * function check_sql( [ string $db ] )
  524.      *      @string $db - Whether or not we should check the validicy of the database
  525.      *
  526.      * Checks if the MySQL credentials are correct
  527.      */
  528.    
  529.     function check_sql($db = true)
  530.     {
  531.         // Connecting to MySQL and capturing the resource handler
  532.         $link = @mysql_connect($this->sql['HOST'], $this->sql['USER'], $this->sql['PASS']);
  533.        
  534.         // Checking if we are not connected
  535.         if($link == false)
  536.         {
  537.             $this->error('SQL_CONNECTION', mysql_errno() . ' - ' . mysql_error());
  538.         }
  539.        
  540.         // Checking if we should select the database
  541.         if($db == true)
  542.         {
  543.             // Selecting the database and capturing the resource handler
  544.             $db = @mysql_select_db($this->sql['DBASE']);
  545.            
  546.             // Checking if there were any errors in the connection
  547.             if($db == false)
  548.             {
  549.                 $this->error('SQL_DB_SELECT', mysql_errno() . ' - ' . mysql_error());
  550.             }
  551.         }
  552.        
  553.         // Closing the MySQL Connection if we are connected
  554.         if($link != false)
  555.         {
  556.             @mysql_close($link);
  557.         }
  558.        
  559.         // Returning the result.
  560.         return (($link != false || $db != false) ? true : false);
  561.     }
  562.    
  563.     /*
  564.      * function get_xml( string $what )
  565.      *      @string $what - The variable to take from an XML File
  566.      *
  567.      * Retrieves a variable from the global file
  568.      */
  569.    
  570.     function get_xml($what)
  571.     {
  572.         // Loading the global XML file
  573.         $db = (array) simplexml_load_file($this->global_file);
  574.        
  575.         // Returing the requested variable to the user
  576.         return (string) $db[$what];
  577.     }
  578.    
  579.     /*
  580.      * function add( string $value [, mix $key] )
  581.      *      @string $value - The value to add to the additional variable
  582.      *      @mix $key - This variable could be set to the following possible values
  583.      *          * 00 (2 zeros) - If you want a numeric, automatically-set keys
  584.      *          * Anything else - If the key is not set to 00, then the key would be what you put it. (string value and key only)
  585.      *
  586.      * Creates an additional array for whatever needs you may need.
  587.      */
  588.    
  589.     function add($value, $key = 00)
  590.     {
  591.         // Checking if the key should be numeric
  592.         if($key == 00)
  593.         {
  594.             // Getting the next available numeric key so we wouldn't over ride a value
  595.             foreach($this->add as $id => $val)
  596.             {
  597.                 // Checking if the key is numeric
  598.                 if(is_numeric($id))
  599.                 {
  600.                     $keyed = $id;
  601.                 }
  602.             }
  603.            
  604.             // Incrementing the last numeric key by one
  605.             $key = $keyed + 1;
  606.            
  607.             // Unsetting the useless $val
  608.             unset($val);
  609.         }
  610.        
  611.         // Generating the session
  612.         $_SESSION['ADD'][$key] = $value;
  613.        
  614.         // Returning true
  615.         return true;
  616.     }
  617.    
  618.     /*
  619.      * function retaval( array $var, int $akey [, boolean $array [, boolean $value [, boolean $numeric]]])
  620.      *      @array $var - The array to retrieve the value/key from
  621.      *      @integer $akey - The key/value to retrieve in numeric terms (1, 2...)
  622.      *      @boolean $array - Determines if the result should come as an array with the key and the
  623.                                 value.
  624.      *      @boolean $value - Set true if you want the value, set false if you want the key...
  625.      *                          set to null if you want an array returned with the key and value
  626.      *      @boolean $numeric - Set to true if you want the key to be numeric... otherwise set
  627.      *                              to false if you want the key to be what it previously was
  628.      *
  629.      * A function to retrieve a value/key from an array. Returns a string by default, or an
  630.      *  array if you need the key to be numeric
  631.      *  
  632.      */
  633.    
  634.     function retaval($var, $akey, $array = false,  $value = false, $numeric = false)
  635.     {
  636.         // Setting some variables
  637.         $i = 0;
  638.         $num = count($var);
  639.        
  640.         // Looping though the array and choosing the key/value that was asked for
  641.         foreach($var as $key => $val)
  642.         {
  643.             // Setting the return value to the current key/value
  644.             if($array == false)
  645.             {
  646.                 $return = (($value == false) ? $key : $val);
  647.             }
  648.            
  649.             // Checking if we need the result as an array or a string
  650.             if($array != false)
  651.             {
  652.                 // Setting the key to be whatever it needs to be
  653.                 $key = (($numeric == false) ? $key : 0);
  654.                
  655.                 // Setting the return array to be whatever it was meant to be
  656.                 $return[$key] = (($value == false) ? ((is_null($value)) ? $value : $key) : $val);
  657.             }
  658.            
  659.             // Making sure we stop when we have the correct key/value
  660.             if($i == $akey)
  661.             {
  662.                 break;
  663.             }
  664.            
  665.             // Incrementing the counter
  666.             ++$i;
  667.         }
  668.        
  669.         //  Returning the return value if there is any
  670.         return (($i > $num) ? false : $return);
  671.     }
  672. }
  673. ?>
Je suppose que je pourrais essayer de l'expliquer rapidement aussi bien...

Im un maniaque commentaire...Je commente à peu près tous seule ligne de code pour expliquer ce que je fais. J'ajoute en peu de rappels et des notes, des crédits si Im le rendant public, le commentaire de tout premier est le nom de fichier et fichier de description.

J'aime utiliser des variables et des noms de classe qui décrit ce qu'ils tiennent / fonction autant que possible avec le moins de mots possible...

J'ai tout tiret et tout garder en conformité avec ce qu'ils sont associés à...thats le mieux que j'ai pu l'expliquer.

Im un perfectionniste, donc si je copier une classe de quelqu'un, peu importe comment elle est grande, je commente tout et tout, ils tiret comme je le souhaite avant que je le télécharge sur mon site
PHP Code: [ Select ]
if ( $something )

Je hais aussi:
PHP Code: [ Select ]
if( $something) { /*code*/ }
if ($something) {
//code
}
if($something )
  1. if( $something) { /*code*/ }
  2. if ($something) {
  3. //code
  4. }
  5. if($something )
\
Qu'est-ce que j'aime, c'est:
PHP Code: [ Select ]
if($cause == $effect && $cause2 == $effect2)
{
     // Some code
}
else
{
     // Some code
}
  1. if($cause == $effect && $cause2 == $effect2)
  2. {
  3.      // Some code
  4. }
  5. else
  6. {
  7.      // Some code
  8. }
J'aime utiliser des accolades à l'intérieur de guillemets pour accéder aux objets et les tableaux. J'aime bien utiliser des guillemets, point final.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • mk27
  • Proficient
  • Proficient
  • Avatar de l’utilisateur
  • Inscription: Juin 09, 2009
  • Messages: 334
  • Status: Offline

Message Septembre 29th, 2009, 12:54 pm

J'utilise le K & R style accolades
Code: [ Select ]
if (whatever) {
  declare this, that;
  ...
}
  1. if (whatever) {
  2.   declare this, that;
  3.   ...
  4. }

mais je suis obligé de tolérer les Allman (ou «tous les blancs»), comme j'aime à l'appeler:

Code: [ Select ]
sub example
(
arg 1
arg 2
)

{
 declare this;
 declare that;

 wonder if there is a program in here?





}
  1. sub example
  2. (
  3. arg 1
  4. arg 2
  5. )
  6. {
  7.  declare this;
  8.  declare that;
  9.  wonder if there is a program in here?
  10. }

La raison pour laquelle je n'aime pas c'est que je préfère le code compressé verticalement afin que je puisse afficher autant de lui comme possible à la fois, tout en conservant le caractère sacré de l'; ligne intacte.

Im court sur les commentaires, mais fort sur l'écriture de code clair avec les noms appropriés variable. J'ai horreur des espaces à la place des onglets et l'idiotie indentation autre.

Une autre chose aussi que je ne souhaite * I * tout le monde aurait est:

Toutes les fonctions des définitions par ordre alphabétique!

Parfois, je vais inclure un commentaire que des listes d'appels possibles, par exemple.
Code: [ Select ]
function test() {  /* called from etc.html and otherfunc() */

comme cela est souvent un indice important pour moi comme pour whats going on.
Image

Afficher de l'information

  • Total des messages de ce sujet: 4 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 160 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC