Answers.Body: <p>We have used the domain in some extensions, like so:</p>
  
  <pre><code>$civiVersion = CRM_Core_BAO_Domain::version();
  
  if (version_compare($civiVersion, '4.5', '&lt;')) {
    //in CiviCRM 4.4 the invoke function has 5 arguments maximum
    return $hook-&gt;invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $fnSuffix);
  } else {
    //in CiviCRM 4.5 and later the invoke function has 6 arguments
    return $hook-&gt;invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, CRM_Utils_Hook::$_nullObject, $fnSuffix);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-11T17:31:32.907Z
Answers.LastActivityDate: 2016-02-11T17:31:32.907Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>So what about keeps the same code but adds a few extra tests to handle the differences more easily:</p>
  
  <ul>
  <li>if it's a new feature we need, test if it exists (eg if function_exists) and include a local copy or workaround of that function if it doesn't (for older versions)</li>
  <li><p>call the new api and check the is_error return, and add a new api/v3 folder in the include path that contains a copy of the missing function and call the api again </p></li>
  <li><p>if it's a missing library (eg. angular or underscore), it's a bit trickier as when you realise that the library is missing, it might be too late as the page is already built and sent to the browser.</p></li>
  </ul>
  
  <p>For that, I'd like to test what civicrm version we have, is </p>
  
  <pre><code>civicrm_api3("domain",get",array("sequential"=&gt;1))[values][0][version] the best way to do that?
  </code></pre>
  
  <p>Can I use the helper functions to compare versions safely with that?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-14T15:52:34.123Z
Answers.Text: Not sure it's best, but in provision_civicrm, CRM_Utils_System::version() is used to get the version
Answers.UserDisplayName: samuelsov
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-12T16:27:57.793Z
Answers.LastActivityDate: 2016-01-12T16:27:57.793Z
Answers.UserDisplayName: Xavier
Answers.Body: <p>at least from 4.4 you can use</p>
  
  <blockquote>
  <pre><code>$result = civicrm_api3('System', 'get', array(
    'sequential' =&gt; 1,
  ));
  </code></pre>
  </blockquote>
  
  <p>which contains the CiviCRM version in the result.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-11T17:14:41.397Z
Answers.LastActivityDate: 2016-02-11T17:34:23.713Z
Answers.LastEditDate: 2016-02-11T17:34:23.713Z
Answers.UserDisplayName: Catorghans
Body: <p>For some extensions, we need to have a different behaviour for different civicrm versions.</p>
  
  <p>I can either manage two branches, but I'll have to backport some fixes and that seems a big overhead. For most of our extensions, I'm tempted for a lighter approach with a single code base</p>
  
Tags: <api><developer>
Title: What's the best way to code for extensions to work for multiple civicrm versions?
ThreadText: # What's the best way to code for extensions to work for multiple civicrm versions?
  
  (Asked by: username_0 on 2016-01-12)
  
  For some extensions, we need to have a different behaviour for different civicrm versions.
  
  I can either manage two branches, but I'll have to backport some fixes and that seems a big overhead. For most of our extensions, I'm tempted for a lighter approach with a single code base
  
  **Tags:** api, developer
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2016-02-11. Score: 1 | Accepted answer
  
  We have used the domain in some extensions, like so:
  
  ```
  $civiVersion = CRM_Core_BAO_Domain::version();
  
  if (version_compare($civiVersion, '4.5', '<')) {
    //in CiviCRM 4.4 the invoke function has 5 arguments maximum
    return $hook->invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $fnSuffix);
  } else {
    //in CiviCRM 4.5 and later the invoke function has 6 arguments
    return $hook->invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, CRM_Utils_Hook::$_nullObject, $fnSuffix);
  }
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2016-01-12. Score: 0
  
  So what about keeps the same code but adds a few extra tests to handle the differences more easily:
  
  * if it's a new feature we need, test if it exists (eg if function\_exists) and include a local copy or workaround of that function if it doesn't (for older versions)
  * call the new api and check the is\_error return, and add a new api/v3 folder in the include path that contains a copy of the missing function and call the api again
  * if it's a missing library (eg. angular or underscore), it's a bit trickier as when you realise that the library is missing, it might be too late as the page is already built and sent to the browser.
  
  For that, I'd like to test what civicrm version we have, is
  
  ```
  civicrm_api3("domain",get",array("sequential"=>1))[values][0][version] the best way to do that?
  
  ```
  
  Can I use the helper functions to compare versions safely with that?
  
  > **Comments:**
  > **username_2**: Not sure it's best, but in provision_civicrm, CRM_Utils_System::version() is used to get the version  
  
  ---
  
  ### Answer by username_3 on 2016-02-11. Score: 0
  
  at least from 4.4 you can use
  
  > ```
  > $result = civicrm_api3('System', 'get', array(
  >   'sequential' => 1,
  > ));
  >
  > ```
  
  which contains the CiviCRM version in the result.
  
  
  ---
  
  