From f06446e2d3d9def2e176260f68ce051c62ef7f87 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 9 Sep 2008 20:36:27 +0000 Subject: --- user_guide/general/styleguide.html | 57 ++++++++------------------------------ 1 file changed, 11 insertions(+), 46 deletions(-) (limited to 'user_guide/general') diff --git a/user_guide/general/styleguide.html b/user_guide/general/styleguide.html index 9d49a9d03..e3c413717 100644 --- a/user_guide/general/styleguide.html +++ b/user_guide/general/styleguide.html @@ -80,7 +80,6 @@ Security
  • Debugging Code
  • Whitespace in Files
  • Compatibility
  • -
  • Use of $SESS->cache
  • Class and File Names using Common Words
  • Database Table Names
  • One File per Class
  • @@ -348,41 +347,7 @@ function build_string($str = "")

    Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 4.3+. Additionally, do not use PHP functions that require non-default libraries to be installed unless your code contains an alternative method when the function is not available, or you implicitly document that your add-on requires said PHP libraries.

    - - -

    Use of $SESS->cache

    -
    -

    $SESS->cache is an array provided for you to use for "flash" content, i.e. values that you would like to persist during a page load, helping you eliminate redundant queries and PHP processing. To avoid conflicts with other first and third-party use of this array, always access it as a multi-dimensional array, using your class name as the primary array name, and your variables within. Naming conventions should follow that of other variables: lowercase letters, underscores for separators between words, and meaningful names.

    - -INCORRECT: -$SESS->cache['admins'] -$SESS->cache['Super_class']['admins'] - -CORRECT: -$SESS->cache['super_class']['admins'] - -

    Here is an example of how one might utilize the $SESS->cache array. This way, no matter how many times this method is called on a given page load (for instance, a tag being used twice on a template, or within a tag that might loop, such as a plugin within the Weblog entries tag), the query and loading of the array occurs only once.

    - -if ( ! isset($SESS->cache['super_class']['admins'])) -{ - $query = $DB->query("SELECT member_id FROM exp_super_class_admins"); - - if ($query->num_rows > 0) - { - foreach ($query->result as $row) - { - $SESS->cache['super_class']['admins'][] = $row['member_id']; - } - } -} - -// set a local variable from the cached global so it's easy to use in the code -$admins = $SESS->cache['super_class']['admins']; - -

    You can see an example of real-world usage of $SESS->cache in the Weblog module's fetch_custom_weblog_fields() and next_prev_entry() methods, and the IP to Nation module's get_country() method.

    -
    -

    Class and File Names using Common Words

    @@ -506,7 +471,7 @@ for ($i = 0; $i < 10; $i++) $arr[ $foo ] = 'foo'; CORRECT: -$arr[$foo] = 'foo'; // no spaces around array keys +$arr[$foo] = 'foo'; // no spaces around array keys INCORRECT: @@ -516,17 +481,17 @@ function foo ( $bar ) } CORRECT: -function foo($bar) // no spaces around parenthesis in function declarations +function foo($bar) // no spaces around parenthesis in function declarations { } INCORRECT: -foreach( $query->result as $row ) +foreach( $query->result() as $row ) CORRECT: -foreach ($query->result as $row) // single space following PHP control structures, but not in interior parenthesis +foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis @@ -632,16 +597,16 @@ $bat = str_replace($foo, $bar, $bag); INCORRECT: // keywords are lowercase and query is too long for // a single line (... indicates continuation of line) -$query = $DB->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses +$query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100"); CORRECT: -$query = $DB->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz - FROM exp_pre_email_addresses - WHERE foo != 'oof' - AND baz != 'zab' - ORDER BY foobaz - LIMIT 5, 100"); +$query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz + FROM exp_pre_email_addresses + WHERE foo != 'oof' + AND baz != 'zab' + ORDER BY foobaz + LIMIT 5, 100"); -- cgit v1.2.3-24-g4f1b