CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • api doco
Pages: [1]

Author Topic: api doco  (Read 1246 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
api doco
January 01, 2011, 04:17:48 pm
Going through the API in eclipse highlights that there are some minor discrepancies that show up in the IDE. Not hugely important but would be good to have a 'preferred'

1)
I see the $params is sometimes passed in with &  and sometimes without - ie.

function civicrm_custom_field_delete( $params )

vs

function civicrm_event_create( &$params )

the second seems more recent so may be preferred although I'm not sure why it makes more sense.


2)

 Also, of the lines at the bottom I assume the first is the best ie.:
- $params mentioned (which it isn't in #2)
- array with no capital
- description says associative array (seems like over-wordiness given that array is specified)

 
 * @param $params     array   Associative array of property name/value pairs to insert in group.
 * @param array id of the group to be deleted
 * @param $params     Array id of the field to be deleted
 
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: api doco
January 02, 2011, 08:37:35 am

1. The second form is preferred, since some of the arrays can be quite big.

2. Yes the first is best. Ideally we give more information on the name/value pairs, especially if the parameters that can be passed in are few/limited

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api doco
January 03, 2011, 01:50:49 am
Like Eileen, I have no idea why we would use &$params, can anyone enlighten us :-)
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: api doco
January 03, 2011, 06:34:03 am
The usual reason to pass param by reference is to save memory (beside the obvious being able to change the variable value).

http://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference

Not convinced how useful it is in the api (the param is not very big in general anyway).

Might be worthwhile doing a benchmark to see if there is a proper benefit (eg. PHP might be smart enough to see that the param is "big" and not modified, so it can avoid to copy the param but only pass the address anyway).

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api doco
January 03, 2011, 06:57:58 am
Thanks X!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: api doco
January 03, 2011, 07:08:06 am

I think it is good programming practice to send in all arrays by reference, and hence makes sense to do so in the API, IMHO

I'm pretty sure PHP is not smart enough and always sends array parameters by value
lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api doco
January 03, 2011, 07:12:25 am
So the standards for API over Xmas became:
* we use civicrm_create_success to return results (and fix it first for the REST, one array level too deep)
* we use params by ref so &$params
* we get rid of the _ internal functions with the same name (so no more civicrm_contact_get and _civicrm_contact_get)
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: api doco
January 03, 2011, 07:40:07 am
Just a note that pass-by-value was invented to protect the caller and promote clean separation of variables to reduce unexpected bugs, i.e. from having their own variables unwittingly changed by third-party code. Yes it comes at the expense of performance.

If the API "promises" not to change input parameter values then that's almost the same, except for unintentional changes made by the API, which may not even be in the API itself, e.g. if you pass off an input parameter to yet some other third-party code which uses pass-by-reference and it changes it, then you may be changing the caller's data without even knowing it.

Callers can protect themselves by making copies, of course.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: api doco
January 04, 2011, 06:46:54 pm
From PHPDoc it looks like

@param $params     array   Associative array of property name/value pairs to insert in group.

should be


@param  array  $params  Associative array of property name/value pairs to insert in group.

Re demeritcowboy's point I notice the case API DOES change a params value - not sure why

    // status msg
    $params['statusMsg'] = ts('Case opened successfully.');
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: api doco
January 08, 2011, 12:40:10 am
Quote from: Eileen on January 04, 2011, 06:46:54 pm
Re demeritcowboy's point I notice the case API DOES change a params value - not sure why

It shouldn't. the API doesn't change the input param. IMO, it's a bug. agree ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api doco
January 08, 2011, 12:57:20 am
Agree, API should not change the input param!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • api doco

This forum was archived on 2017-11-26.