473,388 Members | 1,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 outputHtmlToShowFile($fileName=false) {
if ($fileName) {
// code goes here
} else {
echo "In outputHtmlToShowAFile() 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 outputHtmlToShowFile($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?

Jul 6 '07 #1
4 1503
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 PntFilterFormPart extends PntPagePart {

*// function that will be called */
function setImplicitCombiFilter(&$combiFilter) {

(....)

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

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

(....)

/* class of the actual controller producing the requested page, contais
the errorneous call */
class MclUrenSearchPage extends ObjectSearchPage {
function &getFilterFormPart() {
$part =& parent::getFilterFormPart();

$part->setImplicitCombiFilter(); //parameter missing
//should have passed an instance of PntSqlCombiFilter

return $part;
}

Output from development server:

E_WARNING: 'Missing argument 1 for
PntFilterFormPart::setImplicitCombiFilter(), called in
C:\metaclass\websites\@ndere\webtoko\classes\mcl\p roject\classMclUrenSearchPage.php
on line 9 and defined' in
C:\metaclass\websites\@ndere\webtoko\classes\pnt\w eb\parts\classPntFilterFormPart.php
at line 585
Request params:
'pntType'=>'Uren',
'pntHandler'=>'SearchPage',
'pntScd'=>'d',
'PHPSESSID'=>'66381c07e127a5ab7e3309dc27598741'

class function line
-----------------------------------------------------------
PntFilterFormPart setImplicitCombiFilter
MclUrenSearchPage getFilterFormPart 9
PntObjectSearchPage getRequestedObject 75
PntObjectIndexPage initForHandleRequest 41
PntPage handleRequest 122
PntSite forwardRequest 203
array('pntType'=>'Uren', 'pntHandler'=>'SearchPage', 'pntScd'=>'d',
'PHPSESSID'=>'66381c07e127a5ab7e3309dc27598741')

PntSite handleRequest 194
- - 5 project\index.php
BTW, passing objects by reference is standard procedure in php4. It
excludes the use of defaults.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.
Jul 10 '07 #2
On Jul 10, 3:40 pm, Henk verhoeven <news1@phpPeanuts_RemoveThis.org>
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 PntFilterFormPart extends PntPagePart {

*// function that will be called */
function setImplicitCombiFilter(&$combiFilter) {

(....)

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

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

(....)

/* class of the actual controller producing the requested page, contais
the errorneous call */
class MclUrenSearchPage extends ObjectSearchPage {
function &getFilterFormPart() {
$part =& parent::getFilterFormPart();

$part->setImplicitCombiFilter(); //parameter missing
//should have passed an instance of PntSqlCombiFilter

return $part;

}

Output from development server:

E_WARNING: 'Missing argument 1 for
PntFilterFormPart::setImplicitCombiFilter(), called in
C:\metaclass\websites\@ndere\webtoko\classes\mcl\p roject\classMclUrenSearchPage.php
on line 9 and defined' in
C:\metaclass\websites\@ndere\webtoko\classes\pnt\w eb\parts\classPntFilterFormPart.php
at line 585
Request params:
'pntType'=>'Uren',
'pntHandler'=>'SearchPage',
'pntScd'=>'d',
'PHPSESSID'=>'66381c07e127a5ab7e3309dc27598741'

class function line
-----------------------------------------------------------
PntFilterFormPart setImplicitCombiFilter
MclUrenSearchPage getFilterFormPart 9
PntObjectSearchPage getRequestedObject 75
PntObjectIndexPage initForHandleRequest 41
PntPage handleRequest 122
PntSite forwardRequest 203
array('pntType'=>'Uren', 'pntHandler'=>'SearchPage', 'pntScd'=>'d',
'PHPSESSID'=>'66381c07e127a5ab7e3309dc27598741')

PntSite handleRequest 194
- - 5 project\index.php

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.

Jul 11 '07 #3
On Jul 7, 10:12 pm, gosha bine <stereof...@gmail.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?

Jul 13 '07 #4
On Jul 7, 2:35 am, lawrence k <lkrub...@geocities.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 outputHtmlToShowFile($fileName=false) {
if ($fileName) {
// code goes here
} else {
echo "In outputHtmlToShowAFile() 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 outputHtmlToShowFile($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/

Jul 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil B Brubaker | last post by:
I'm trying to create my own shellexecute program named shell4OD and am having problems with handles. Here is my code ... ===== Private Declare Function ShellExecute Lib _ "shell32.dll" Alias...
65
by: Nikolas Hagelstein | last post by:
Hi, First of all: i spend a lot of time on thinking and researching about how to make developing webapplications with php a more structured/systematic thing. I even fancied to switch over to...
17
by: Billy Patton | last post by:
In c can I declare a default value for an function argument? int X(int a,int b=10); int main() { X(1); } int X(int a,int b)
0
by: **Developer** | last post by:
I know this is not the correct NG but I've been trying to get this answered for a while and can't find someone really knowledgeable about printers. (Actually it does relate to something I'm...
0
by: kellyonlyone | last post by:
E-XD++ MFC Library Professional Edition V9.20 is released (100% Source Code)! -------------------------------------------------------------------------------- February 18, 2006 For more...
5
by: Dinesh Kumar | last post by:
Hi all I am using VB.NET for a Connector dll in Delphi client and some webservice . can you tell me how to handle pointers in Vb.net which are passed by delphi client as parameters in function...
4
by: Bob Stearns | last post by:
Consider a function header: function init($name, $ttl, $need_bhid="N", $need_entity_id="N") What values do the last two parameters have when it is called with the following arguments: ...
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.