Click to See Complete Forum and Search --> : php coding style


emus
01-25-2006, 09:33 PM
If I have php script containing several functions, should I have an area at the beginning or end of the script where all function calls are being made and values returned to before calling a new function, or ist it preferable to call functions from within functions?

Thanks a lot,

emus

cybertron
01-25-2006, 11:51 PM
Well, if I understand your question correctly I don't think it makes a lot of difference. It's more of a style issue, although if you are repeatedly using the value returned from a function it might be better to call it once and assign it to a variable rather than having the overhead of a number of function calls. I think in general though, people just call functions from within functions, and there's nothing wrong with that.

bwkaz
01-26-2006, 07:53 PM
Heck, I define functions inside other functions, so simply calling them is definitely no big deal. :p

(I don't do it in PHP, though. Actually I doubt it would even be possible to do in PHP, though I don't know for sure. I can do it in Lisp, ECMAscript, and Python. I bet Perl can do it, but I don't do much Perl.)

(In case you're wondering why: It's usually done for a similar reasons that full classes get used in "wimpier" languages. If you need to pass a callback function to something else (such as window.timeout() in ECMAscript, or a PyGtk function in Python), but you want that callback to have access to some of the local variables in the function that you're currently in, you can do that with an "inner" function. But the language has to have lexical scope and first-class functions; otherwise it won't really help.)

thaddaeus
01-27-2006, 02:02 AM
For organization purposes I have an inlcude dir which contains a single include file which includes other files containing functions. basicaly each function gets its own file such as user_name.f.php for a function or if I have a set of functions for some purpose lets say user admin, then I'd place all the releated function in the file and name it user_admin.fs.php

I then do an include_once('<function_nameOrset.f/fs.php>'), the include_once stops errors if the file may have been included previously by accident. This way all functions are in a single location and keeps the main site more organized.

chameleon
01-27-2006, 08:05 AM
I agree with thaddaeus on putting the functions in an include file, it keeps all the functions in a library and leaves the other code clutter free.

emus
01-27-2006, 05:52 PM
Thanks for the replies everyone. I'm still quite new to php and my code looked really cluttered to me, that's why I was wondering if there was a better way.

thaddaeus
01-27-2006, 07:31 PM
If you want some strict coding examples check out the code/manuals for the phpbb system. phpbb.com . If you look through their code minus the template files, everthing else from mods to the actual pages follow a strict codeing pattern that in all, makes it easier to understand everything.

emus
01-28-2006, 03:18 PM
Thanks a lot, thaddaeus, I'll check it out.