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 [2007/12/22 14:30] – external edit 127.0.0.1coding [2020/11/23 17:23] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * OSInet contributions to Drupal    * OSInet contributions to Drupal 
     * Contributions to Drupal core follow the [[http://drupal.org/node/318|Drupal coding standard]].     * Contributions to Drupal core follow the [[http://drupal.org/node/318|Drupal coding standard]].
 +    * Contributions to non-OSInet contributed code follow the coding convention of said code, usually the same Drupal coding standard as core.
     * Contributions outside core follow the here-defined OSInet coding style.     * Contributions outside core follow the here-defined OSInet coding style.
   * Contributions to PEAR follow the [[http://pear.php.net/manual/en/standards.php|PEAR coding standards]]   * Contributions to PEAR follow the [[http://pear.php.net/manual/en/standards.php|PEAR coding standards]]
Line 11: Line 12:
 Examples on this page are given in the context of a module (Drupal) or class called G2. Examples on this page are given in the context of a module (Drupal) or class called G2.
  
-All code must work under error_reporting(E_ALL | E_STRICT);+All code must work under error_reporting(E_ALL | E_STRICT)
  
 ====== Indenting ====== ====== Indenting ======
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
  
Line 33: 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 64: Line 73:
 </code> </code>
  
-===== Class Declarations ===== 
  
 +
 +
 +===== Class Declarations =====
  
 <html> <html>
Line 75: 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 101: 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 113: 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 122: 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 129: 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 136: Line 139:
 } else { } else {
    defaultaction;    defaultaction;
-}</td> +}</td></tr>
-    </tr>+
   <tr>   <tr>
     <td>&nbsp;</td>     <td>&nbsp;</td>
Line 149: Line 151:
  
 For OSInet, long tests must be wrapped to align readably. Although ZF does not mention this, the line length requirement makes it likely too. For OSInet, long tests must be wrapped to align readably. Although ZF does not mention this, the line length requirement makes it likely too.
 +
  
 ===== switch ===== ===== switch =====
Line 160: 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 174: 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 187: 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 200: 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 210: Line 209:
     default:     default:
         break;         break;
-}</td> +}</td></tr>
-    </tr>+
   </table>   </table>
 </html> </html>
 +
  
 ===== General table ===== ===== General table =====
Line 224: Line 223:
 ^ ... control, closing |  = ZF  |  n.a.  || The closing brace is always written on its own line.  | ^ ... control, closing |  = ZF  |  n.a.  || The closing brace is always written on its own line.  |
 ^ ... classes |  = ZF  |  n.a.  || The brace is always written on the line underneath the class name.  | ^ ... classes |  = ZF  |  n.a.  || The brace is always written on the line underneath the class name.  |
 +
 +
 +
 +
 +
  
  
Line 243: Line 247:
  
 Spacing, one-liner: **identical** Spacing, one-liner: **identical**
- 
 Spacing, indented: **Different** Spacing, indented: **Different**
  
Line 261: Line 264:
 Additional comma: identical. Additional comma: identical.
  
 +====== Including Code ======
  
  
- 
-====== Including Code ====== 
  
 OSInet contrib code does not support PHP4, so PHP5 structures like the new object model are standard. OSInet contrib code does not support PHP4, so PHP5 structures like the new object model are standard.
 +
 +
  
 This means that including can make use of the autoload mechanism, which can be more efficient under caching than using require_once with conditional content. This means that including can make use of the autoload mechanism, which can be more efficient under caching than using require_once with conditional content.
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
  
Line 290: Line 304:
  
 The rule regarding the closing ?> tag is only applicable to "program-type" PHP files, not to HTML pages with embedded PHP, which are mostly out of the scope of these rules. The rule regarding the closing ?> tag is only applicable to "program-type" PHP files, not to HTML pages with embedded PHP, which are mostly out of the scope of these rules.
- 
- 
- 
- 
  
  
Line 380: Line 390:
  
 ====== Using CVS ====== ====== Using CVS ======
 +
 +
  
 **Identical** **Identical**
 +
 +
  
 Additional rule: modules must define a constant containing the version information to be displayed on the module settings page. See the rules for constants below. Additional rule: modules must define a constant containing the version information to be displayed on the module settings page. See the rules for constants below.
 +
 +
  
 ====== Example URLs ======  ====== Example URLs ====== 
 +
 +
  
 **Identical** **Identical**
 +
 +
 +
  
  
 ====== Naming Conventions ====== ====== Naming Conventions ======
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
  
Line 401: Line 434:
  
 ===== General ===== ===== General =====
 +
 <html> <html>
   <table class="inline" style="width: 100%">   <table class="inline" style="width: 100%">
Line 413: Line 447:
     <tr style="vertical-align: top">     <tr style="vertical-align: top">
       <td>Functions</td>       <td>Functions</td>
-      <td style="font-family: monospace">getElementById()</td>+      <td>as ZF</td>
       <td style="font-family: monospace">g2_get_element_by_id()</td>       <td style="font-family: monospace">g2_get_element_by_id()</td>
       <td style="font-family: monospace">?</td>       <td style="font-family: monospace">?</td>
Line 530: Line 564:
     </table>     </table>
   </html>   </html>
- 
- 
  
  
 ===== Variables ===== ===== Variables =====
  
-Variables naming following the rules for functions and methods. +Variables naming following the rules for functions and methods. Exceptions:
- +
-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
Line 561: Line 591:
   * 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.
- 
- 
- 
- 
  
  
Line 577: Line 603:
  
 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
Line 584: Line 611:
   }   }
 </code> </code>
 +
  
 ===== Drupal-specific: Naming functions and constants within modules ===== ===== Drupal-specific: Naming functions and constants within modules =====
Line 608: Line 636:
 My_Module::foo(My_Module::SOME_CONSTANT); My_Module::foo(My_Module::SOME_CONSTANT);
 </code> </code>
- 
coding.txt · Last modified: 2020/11/23 17:23 by 127.0.0.1