User Tools

Site Tools


coding

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
coding [2008/04/17 22:07] fgmcoding [2020/11/23 17:23] (current) – external edit 127.0.0.1
Line 45: Line 45:
     <th>ZF</th>     <th>ZF</th>
     </tr>     </tr>
-  <tr style="white-space: pre; font-family: monospace;"> +  <tr style="white-space: pre; font-family: monospace;"><td style="vertical-align: top">private function foo($bar, $baz = 'qux'
-    <td style="vertical-align: top">private function foo($bar, $baz = 'qux'+
   {   {
   $ret = action;   $ret = action;
   return $ret;   return $ret;
-  }</td> +  }</td><td style="vertical-align: top">function _g2_foo($bar, $baz = 'qux') {
-    <td style="vertical-align: top">function _g2_foo($bar, $baz = 'qux') {+
   return action;   return action;
-}</td> +}</td><td colspan="2" style="vertical-align: top">private function foo($bar, $baz = 'qux')
-      <td colspan="2" style="vertical-align: top">private function foo($bar, $baz = 'qux')+
     {     {
         return action;         return action;
Line 89: Line 86:
     <th>ZF</th>     <th>ZF</th>
     </tr>     </tr>
-  <tr style="white-space: pre; font-family: monospace;"> +  <tr style="white-space: pre; font-family: monospace;"><td style="vertical-align: top">class Foo
-    <td style="vertical-align: top">class Foo+
   {   {
   // members   // members
-  }</td> +  }</td><td style="vertical-align: top">N.A: no classes</td><td>?</td><td style="vertical-align: top">class Foo
-    <td style="vertical-align: top">N.A: no classes</td> +
-      <td>?</td> +
-    <td style="vertical-align: top">class Foo+
 { {
     // members     // members
Line 115: Line 108:
     <th>ZF</th>     <th>ZF</th>
     </tr>     </tr>
-  <tr style="white-space: pre; font-family: monospace;"> +  <tr style="white-space: pre; font-family: monospace;"><td style="vertical-align: top">if (test1 || test2)
-    <td style="vertical-align: top">if (test1 || test2)+
   {   {
   action1;   action1;
Line 127: Line 119:
   {   {
   defaultaction;   defaultaction;
-  }</td> +  }</td><td style="vertical-align: top">if (test1 || test2) {
-    <td style="vertical-align: top">if (test1 || test2) {+
   action1;   action1;
 } }
Line 136: Line 127:
 else { else {
   defaultaction;   defaultaction;
-}</td> +}</td><td style="vertical-align: top">if ((test1) || (test2)) {
-    <td style="vertical-align: top">if ((test1) || (test2)) {+
     action1;     action1;
 } elseif ((test3) && (test4)) { } elseif ((test3) && (test4)) {
Line 143: Line 133:
 } else { } else {
     defaultaction;     defaultaction;
-}</td> +}</td><td style="vertical-align: top">if (test1) {
-    <td style="vertical-align: top">if (test1) {+
     action1;     action1;
 } else if (test3) { } else if (test3) {
Line 150: Line 139:
 } else { } else {
    defaultaction;    defaultaction;
-}</td> +}</td></tr>
-    </tr>+
   <tr>   <tr>
     <td>&nbsp;</td>     <td>&nbsp;</td>
Line 175: Line 163:
     <th>ZF</th>     <th>ZF</th>
     </tr>     </tr>
-    <tr style="white-space: pre; font-family: monospace"> +    <tr style="white-space: pre; font-family: monospace"><td style="vertical-align: top">switch (condition) 
-      <td style="vertical-align: top">switch (condition) +
   {   {
   case 1:   case 1:
Line 189: Line 176:
     defaultaction;     defaultaction;
     break;     break;
-  }</td> +  }</td><td style="vertical-align: top">switch (condition) {
-      <td style="vertical-align: top">switch (condition) {+
   case 1:   case 1:
     action1;     action1;
Line 202: Line 188:
     defaultaction;     defaultaction;
     break;     break;
-}</td> +}</td><td style="vertical-align: top">switch (condition) {
-      <td style="vertical-align: top">switch (condition) {+
 case 1: case 1:
     action1;     action1;
Line 215: Line 200:
     defaultaction;     defaultaction;
     break;     break;
-}</td> +}</td><td style="vertical-align: top">switch (condition) {
-      <td style="vertical-align: top">switch (condition) {+
     case 1:     case 1:
         break;         break;
Line 225: Line 209:
     default:     default:
         break;         break;
-}</td> +}</td></tr>
-    </tr>+
   </table>   </table>
 </html> </html>
Line 581: Line 564:
     </table>     </table>
   </html>   </html>
 +
  
 ===== Variables ===== ===== Variables =====
  
- +Variables naming following the rules for functions and methods. Exceptions:
- +
-Variables naming following the rules for functions and methods. +
- +
- +
- +
-Exceptions: +
- +
  
   * existing libraries or APIs are not renamed. Facade APIs may be used until existing libraries match the new format   * existing libraries or APIs are not renamed. Facade APIs may be used until existing libraries match the new format
- 
   * the standard variable $ret should be used for all return values. Always use it to return a value, instead of directly returning the latest instruction result. This helps with a typical debugger configuration: breakpoint on return and $ret as a watched value.   * the standard variable $ret should be used for all return values. Always use it to return a value, instead of directly returning the latest instruction result. This helps with a typical debugger configuration: breakpoint on return and $ret as a watched value.
- 
   * some Hungarian notation is used:    * some Hungarian notation is used: 
- 
     * the name of "unsafe" strings is prefixed by "us_"     * the name of "unsafe" strings is prefixed by "us_"
- 
     * http://www.joelonsoftware.com/articles/Wrong.html     * http://www.joelonsoftware.com/articles/Wrong.html
- 
   * naming resulting from external objects applies the rule by pieces. Example:   * naming resulting from external objects applies the rule by pieces. Example:
- 
     * GTK widgets are named <abbreviated class><Instance> within Glade     * GTK widgets are named <abbreviated class><Instance> within Glade
- 
     * GTK signals are called <signal>     * GTK signals are called <signal>
- 
     * Related callbacks are called on_<abbreviated class><Instance>_<signal>     * Related callbacks are called on_<abbreviated class><Instance>_<signal>
- 
     * Example: on_fmMain_destroy is      * Example: on_fmMain_destroy is 
- 
       * "on_" : the callback for...       * "on_" : the callback for...
- 
       * "fmMain" : a Main Form GtkWindow widget       * "fmMain" : a Main Form GtkWindow widget
- 
       * "_destroy" : the "destroy" GTK signal       * "_destroy" : the "destroy" GTK signal
- 
     * Rationale: forcing CamelCaps for the standard naming convention would result in onFmMainDestroy, which splits ambiguously and is not a default in Glade, and with Fm being capitalized inconsistently in various places in the app. As an instance instead of a class, it should remain lowercase     * Rationale: forcing CamelCaps for the standard naming convention would result in onFmMainDestroy, which splits ambiguously and is not a default in Glade, and with Fm being capitalized inconsistently in various places in the app. As an instance instead of a class, it should remain lowercase
- 
- 
- 
  
  
 ===== Functions and Methods ===== ===== Functions and Methods =====
- 
- 
  
   * Since PHP4 support is not required, visibility (private, protected) settings on class members are recommended when using PHP5 class constructions.   * Since PHP4 support is not required, visibility (private, protected) settings on class members are recommended when using PHP5 class constructions.
- 
   * functions or methods returning unsafe values are prefixed by "us_" just like variables, as "us_someFunctionName()"   * functions or methods returning unsafe values are prefixed by "us_" just like variables, as "us_someFunctionName()"
- 
   * "final" classes should not contain "protected" members, since they can not have derived classes which would take advantage of the "protected" access.   * "final" classes should not contain "protected" members, since they can not have derived classes which would take advantage of the "protected" access.
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
  
  
 ===== Constants ===== ===== Constants =====
- 
- 
  
 As of 01/01/2007, see table above. Note that the PHP builtins "null", "true", and "false" are lowercase, unlike constants from user code. As of 01/01/2007, see table above. Note that the PHP builtins "null", "true", and "false" are lowercase, unlike constants from user code.
- 
- 
  
 Previously, the format was: Previously, the format was:
- 
 <code php> <code php>
- 
 define('G2VERSION', '$Id'); define('G2VERSION', '$Id');
- 
 </code> </code>
- 
- 
  
 This style of coding is now considered obsolete (even under Drupal conventions, the constant should be named G2_VERSION, not G2VERSION), and should be replaced when preparing module versions for Drupal 6 by class constants like G2::VERSION, or more generally Foo::SOME_CONSTANT : This style of coding is now considered obsolete (even under Drupal conventions, the constant should be named G2_VERSION, not G2VERSION), and should be replaced when preparing module versions for Drupal 6 by class constants like G2::VERSION, or more generally Foo::SOME_CONSTANT :
  
 <code php> <code php>
- 
 class G2 class G2
- 
   {   {
- 
   // don't forget the PHPdoc here   // don't forget the PHPdoc here
- 
   const VERSION = '$Id$';   const VERSION = '$Id$';
- 
   }   }
- 
 </code> </code>
- 
  
  
 ===== Drupal-specific: Naming functions and constants within modules ===== ===== Drupal-specific: Naming functions and constants within modules =====
- 
- 
  
   * Drupal mandates some function names, notably, for module "mymodule":   * Drupal mandates some function names, notably, for module "mymodule":
- 
     * default theme functions must be named theme_mymodule_foo      * default theme functions must be named theme_mymodule_foo 
- 
     * hook_foo implementations must be named mymodule_foo     * hook_foo implementations must be named mymodule_foo
- 
   * Beyond that, all other functions in a module should be defined as static public methods of a class named like the module, like:   * Beyond that, all other functions in a module should be defined as static public methods of a class named like the module, like:
- 
 <code php> <code php>
- 
 // don't forget the PHPdoc comments here // don't forget the PHPdoc comments here
- 
 class My_Module class My_Module
- 
   {   {
- 
   // and here   // and here
- 
   const SOME_CONSTANT = 'bar';   const SOME_CONSTANT = 'bar';
- 
- 
  
   // ... and here too   // ... and here too
- 
   static public function foo($op)   static public function foo($op)
- 
     {     {
- 
     // do something and return     // do something and return
- 
     }     }
- 
   }   }
- 
- 
  
 // ..which can be invoked as: // ..which can be invoked as:
- 
 My_Module::foo(My_Module::SOME_CONSTANT); My_Module::foo(My_Module::SOME_CONSTANT);
- 
 </code> </code>
- 
- 
coding.1208470036.txt.gz · Last modified: 2020/11/23 17:23 (external edit)