addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupsimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1outlookpersonStartprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

Vienna PHP Community Message Board › How to serve a function with parameters?

How to serve a function with parameters?

Stefan H.
user 107614972
Vienna, AT
Post #: 1
Hello, folks!

Alex has told in the august meetup a little bit about "whats going on in php develoment" and we have talked a while about an RFC for PHP 6.0. This is the base for my question:

How can I serve a function with parameters?

If i have to make a code review or to integrate somebodies code, often i have some problems to understand functions. I will give you an example:

convert_csv_to_xml('filename1', 'filename2', 5, 42, 'On')

I think, that everybody could understand, what this function has to to. But what the parameters are standing for? I will not be very happy and efficient, if I have to enter the librarycode with the function definition or the UnitTest-Code for understanding these parameters. And remember, I am not the developer, I have to do a code review (or maintenance job).

One proposal for a better way may be:

$sourcefile = 'filename1';
$targetfile = 'filename2';
$readfromline = 5;
$readuntilline = 42;
$syntaxcheck = 'On';

convert_csv_to_xml($sourcefile, $targetfile, $readfromline, $readuntilline, $syntaxcheck);

Have you any ideas or suggestions, how we can handle this in PHP in a good way?

@alex: Could you please post the RFC and tell us a little bit about the background and the idea behind it? thanks!
Benedikt D.
user 115680822
Vienna, AT
Post #: 1
Hi, here are my thoughts:

Code Insight helps a lot, if you have a decent IDE (I use NuSphere PhpEd), as long as the developer comments on the function, which she should do anyways.

Particular in OOP I find Code Insight extremely useful and one of the main reasons for using an IDE (the other beeing integrated FTP and SVN).

If you don't use an IDE you could go for:

convert_csv_to_xml($sourcefile = 'filename1',
$targetfile = 'filename2',
$readfromline = 5,
$readuntilline = 42,
$syntaxcheck = 'On');

Which is basically the same what you wrote but shorter and easier to read IMHO.
Stefan H.
user 107614972
Vienna, AT
Post #: 2
Hi, Ben!

Thanks for you post. I have tested it and it works. Unfortunately mixing the position of the parameters doesn't work:

$targetfile = 'filename2', // <--- 2
$sourcefile = 'filename1', // <--- 1
$readfromline = 5,
$readuntilline = 42,
$syntaxcheck = 'On');

Also empty parameters, which have standard values predefined in the function definition, would not work.

$sourcefile = 'filename1',
$targetfile = 'filename2',
// Standard value predifined $readfromline = 0
$readuntilline = 42,
$syntaxcheck = 'On');

How can we solve this?

I also have heard some proposals, to use arrays or json- or yaml-strings for giving parameters to a function like

$parameter = (
'sourcefile' => 'filename1',
'targetfile' => 'filename2',
'readfromline' => 5,
'readuntilline' => 42,
'syntaxcheck' => 'On'


Does this make sense?
A former member
Post #: 1
Whatever is there in the RFC for the future will not solve your problem right now.
Usually I do just like Benedict, explicitly naming the value at the invoking side, if necessary.

Generally, it's a bad practice to have more then 2 or 3 arguments to a function. In your specific case this ideally should be a class with a constructor that takes a source file, target file and a method which takes the start/end. The syntackCheck should be a default class behavior with a method to explicitly turn it on/off ->checkSyntax(true).

In any case, whatever you do, you can't have optional arguments omitted in the middle of a function invokation.
The proposal for named arguments, which would let you do that, or reorder arguments is available here:

rfc: named_params
The problem is this is the second time they are proposed
The first time was here:
rfc:namedparameters and it was already declined once, therefore I don't see much reason to believe it will actually happen, but everything is possible.

P.S. Avoid arrays for that case at any cost.
Arrays like this will increase greatly the chance to make something wrong. You cannot guarantee presence of a key in an array. A developer might accidentally name it differently, forget to pass one, IDE wouldn't support this or auto complete it and no QA tools will be able to analyze your code properly.
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy