how do professional PHP developers handle defaults for function parameters?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence k

    how do professional PHP developers handle defaults for function parameters?


    I'm working in PHP 4. Even if a parameter is mandatory, I nearly
    always give it a default value of "false". Then I print an error
    message if someone who is calling my function forgot the mandatory
    parameter:

    function outputHtmlToSho wFile($fileName =false) {
    if ($fileName) {
    // code goes here
    } else {
    echo "In outputHtmlToSho wAFile() the code expected the first
    parameter to be a file name, but instead the value was false";
    }
    }

    How do others handle default parameter values?

    I've been avoiding this:

    function outputHtmlToSho wFile($fileName ) { }

    because if I do this and someone forgets to pass a file name to the
    function, then the PHP parser issues an error. I've been assuming
    that relying on the PHP parser to write my error messages for me is
    unprofessional. Does anyone else feel that way?

  • Henk verhoeven

    #2
    Re: how do professional PHP developers handle defaults for functionparamet ers?

    lawrence k wrote:
    How do others handle default parameter values?
    I guess you mean "how do others handle missing parameters?"

    Well, on the development server my error handler will produce a walkback
    so that i can see where the call was coming from.

    Example:

    /** class of the user interface component being called upon
    class PntFilterFormPa rt extends PntPagePart {

    *// function that will be called */
    function setImplicitComb iFilter(&$combi Filter) {

    (....)

    /** Front controller class /*
    class Site extends PntSite {

    function setErrorHandler () {
    includeClass('E rrorHandler');
    $this->errorHandler =& new ErrorHandler();
    $this->errorHandler->startHandling( );
    }

    (....)

    /* class of the actual controller producing the requested page, contais
    the errorneous call */
    class MclUrenSearchPa ge extends ObjectSearchPag e {
    function &getFilterFormP art() {
    $part =& parent::getFilt erFormPart();

    $part->setImplicitCom biFilter(); //parameter missing
    //should have passed an instance of PntSqlCombiFilt er

    return $part;
    }

    Output from development server:

    E_WARNING: 'Missing argument 1 for
    PntFilterFormPa rt::setImplicit CombiFilter(), called in
    C:\metaclass\we bsites\@ndere\w ebtoko\classes\ mcl\project\cla ssMclUrenSearch Page.php
    on line 9 and defined' in
    C:\metaclass\we bsites\@ndere\w ebtoko\classes\ pnt\web\parts\c lassPntFilterFo rmPart.php
    at line 585
    Request params:
    'pntType'=>'Ure n',
    'pntHandler'=>' SearchPage',
    'pntScd'=>'d',
    'PHPSESSID'=>'6 6381c07e127a5ab 7e3309dc2759874 1'

    class function line
    -----------------------------------------------------------
    PntFilterFormPa rt setImplicitComb iFilter
    MclUrenSearchPa ge getFilterFormPa rt 9
    PntObjectSearch Page getRequestedObj ect 75
    PntObjectIndexP age initForHandleRe quest 41
    PntPage handleRequest 122
    PntSite forwardRequest 203
    array('pntType' =>'Uren', 'pntHandler'=>' SearchPage', 'pntScd'=>'d',
    'PHPSESSID'=>'6 6381c07e127a5ab 7e3309dc2759874 1')

    PntSite handleRequest 194
    - - 5 project\index.p hp


    BTW, passing objects by reference is standard procedure in php4. It
    excludes the use of defaults.

    Greetings,

    Henk Verhoeven,
    www.phpPeanuts.org.

    Comment

    • pangea33

      #3
      Re: how do professional PHP developers handle defaults for function parameters?

      On Jul 10, 3:40 pm, Henk verhoeven <news1@phpPeanu ts_RemoveThis.o rg>
      wrote:
      lawrence k wrote:
      How do others handle default parameter values?
      >
      I guess you mean "how do others handle missing parameters?"
      >
      Well, on the development server my error handler will produce a walkback
      so that i can see where the call was coming from.
      >
      Example:
      >
      /** class of the user interface component being called upon
      class PntFilterFormPa rt extends PntPagePart {
      >
      *// function that will be called */
      function setImplicitComb iFilter(&$combi Filter) {
      >
      (....)
      >
      /** Front controller class /*
      class Site extends PntSite {
      >
      function setErrorHandler () {
      includeClass('E rrorHandler');
      $this->errorHandler =& new ErrorHandler();
      $this->errorHandler->startHandling( );
      }
      >
      (....)
      >
      /* class of the actual controller producing the requested page, contais
      the errorneous call */
      class MclUrenSearchPa ge extends ObjectSearchPag e {
      function &getFilterFormP art() {
      $part =& parent::getFilt erFormPart();
      >
      $part->setImplicitCom biFilter(); //parameter missing
      //should have passed an instance of PntSqlCombiFilt er
      >
      return $part;
      >
      }
      >
      Output from development server:
      >
      E_WARNING: 'Missing argument 1 for
      PntFilterFormPa rt::setImplicit CombiFilter(), called in
      C:\metaclass\we bsites\@ndere\w ebtoko\classes\ mcl\project\cla ssMclUrenSearch Page.php
      on line 9 and defined' in
      C:\metaclass\we bsites\@ndere\w ebtoko\classes\ pnt\web\parts\c lassPntFilterFo rmPart.php
      at line 585
      Request params:
      'pntType'=>'Ure n',
      'pntHandler'=>' SearchPage',
      'pntScd'=>'d',
      'PHPSESSID'=>'6 6381c07e127a5ab 7e3309dc2759874 1'
      >
      class function line
      -----------------------------------------------------------
      PntFilterFormPa rt setImplicitComb iFilter
      MclUrenSearchPa ge getFilterFormPa rt 9
      PntObjectSearch Page getRequestedObj ect 75
      PntObjectIndexP age initForHandleRe quest 41
      PntPage handleRequest 122
      PntSite forwardRequest 203
      array('pntType' =>'Uren', 'pntHandler'=>' SearchPage', 'pntScd'=>'d',
      'PHPSESSID'=>'6 6381c07e127a5ab 7e3309dc2759874 1')
      >
      PntSite handleRequest 194
      - - 5 project\index.p hp
      >
      BTW, passing objects by reference is standard procedure in php4. It
      excludes the use of defaults.
      >
      Greetings,
      >
      Henk Verhoeven,www.phpPeanuts.org.

      There was a lot of good feedback in this thread, and I am not the sort
      of self-absorbed developer who is closed to the viewpoints of others.
      A lot of you have made me rethink portions of my original post. Thanks
      for keeping it clean and for giving some alternatives rather than
      simply bashing. Hopefully some others will weigh in on the subject too.

      Comment

      • lawrence k

        #4
        Re: how do professional PHP developers handle defaults for function parameters?

        On Jul 7, 10:12 pm, gosha bine <stereof...@gma il.comwrote:
        pangea33 wrote:
        My post got longer and longer as my thoughts came together on this
        subject. The gist of my claim is that you shouldn't build something as
        FRAGILE as a function that breaks if there are argument variations,
        when you can simply build a more robust function in the first place.
        >
        If something is going to fail, let it fail. The sooner it fails, the
        more chances to discover and fix the bug.
        That's an interesting reply. Do you do any error checking at all in
        your code? If so, what do you error check for?

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: how do professional PHP developers handle defaults for function parameters?

          On Jul 7, 2:35 am, lawrence k <lkrub...@geoci ties.comwrote:
          I'm working in PHP 4. Even if a parameter is mandatory, I nearly
          always give it a default value of "false". Then I print an error
          message if someone who is calling my function forgot the mandatory
          parameter:
          >
          function outputHtmlToSho wFile($fileName =false) {
          if ($fileName) {
          // code goes here
          } else {
          echo "In outputHtmlToSho wAFile() the code expected the first
          parameter to be a file name, but instead the value was false";
          }
          >
          }
          >
          How do others handle default parameter values?
          >
          I've been avoiding this:
          >
          function outputHtmlToSho wFile($fileName ) { }
          >
          because if I do this and someone forgets to pass a file name to the
          function, then the PHP parser issues an error. I've been assuming
          that relying on the PHP parser to write my error messages for me is
          unprofessional. Does anyone else feel that way?
          IMHO, the default parameters options are for handling the
          situation to have default values; it's not meant to professionally
          handle exception of missing params.

          If you define a function with default params in it, it would
          usually mean that the function can be called without any params for
          it's default behavior. So, if you want to handle missing params, use
          exception/error handler.

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          Working...