Log in

View Full Version : Resolved where should functions be placed?



james438
06-10-2011, 12:12 AM
After working with javascript a little bit now I feel ready to try my hand again at php functions. It looks like with javascript functions are stored in the head of the page. With php where should the defined function be placed?

fastsol1
06-10-2011, 12:26 AM
Most would likely say in a separate folder and separate file. Then include the file where needed.

james438
06-10-2011, 12:36 AM
Maybe so, but when a page loads it won't matter where the script is located so long as it is loaded. Should a php function be appropriately located in the head, body, footer, title, before the function is called or does it matter?

fastsol1
06-10-2011, 01:32 AM
I always load them towards the top of the code. With php there is no loading in the head or body or where ever cause that has no bearing in php. Php loads from top to bottom of the page and that's really all you need to concern yourself with. With functions they can be called from anywhere in the page but you must have included them beforehand to be able to use them.

I am currently building a CMS and within the first few lines I have a foreach that includes all the needed function files before anything else cause directly after that those functions are used to get settings and all sorts of other things.

djr33
06-10-2011, 01:50 AM
Short answer: define your functions separately, before any of the content.

Personally I prefer to create a function library in separate files, and if the site is big enough I have several function definition pages (including classes, perhaps system settings and other things too) all loaded through a common base configuration page. For smaller projects or one-page scripts, I include all functions at the top then do all of the content and other things after that. Either way, functions are separated from the rest.

To some degree, there's no technical reason that this organization is required, but I strongly prefer it so I can keep everything organized in my mind. If you aren't being very careful with a script, it's not that bad to define things in sections, as long as you place functions before they are actually used.

I was under the impressions that functions MUST be defined before they are called. That's different than Javascript, where Javascript reads everything and does not (at least not only) operate procedurally from the top of the code to the bottom.
However, the following code actually works just fine:

<?php
hello();
function hello() { echo 'hello'; }
?>
I strongly recommend against that though. It's confusing, not helpful, and CAN get you intro trouble if things get complicated.


I think the basic explanation is this: PHP defines functions when it gets to that line to process them. I know this is the case because you can have functions defined inside conditional (if) statements and they only get defined if that conditional statement is evaluated. But then there is an exception as shown by the example above: apparently they are also pre-processed if they are in global, immediately accessible scope. So I do think that's an exception, not the standard way.

Here's a demonstration of how that may work:

<?php
hello();
if (1==1) { function hello() { echo 'hello'; } }
?>
Even though 1==1 always evaluates as true, PHP doesn't parse the IF until it runs-- but it jumps ahead and parses global-scope functions apparently.


The only downside to defining functions early is that they take up memory. So I guess technically if you have a huge function you will only need to run at the end of every page, defining it early will waste storage (and processing?) if it's parsed early rather than later on the page. But of course at that point you'd need to embed it somehow so it's not in the global scope mentioned above.


Complications:
1. Functions can only be defined once and can never be changed. For this reason, doing anything complex is probably not that relevant.
2. In embedded contexts such as ifs or loops, functions may actually be variably (or not) defined. This gets really messy and it's usually a bad idea-- better to handle the if within the function than to define it two alternative ways, but it is possible.
3. Very importantly, if you are including pages, includes do NOT execute until that line is reached in parsing. So that function will NOT be available for previous uses. For this reason alone, it makes sense to always define functions first, in case you ever split a page in two using includes. Interestingly, this is one way that includes don't just "merge" two pages seemlessly. In terms of code, they effectively do that, but apparently not in terms of parsing that code originally.


Hope that answers your question.

traq
06-10-2011, 03:53 AM
Something that is important here is the way you think about php.

javascript, obviously, is "part of" your webpage. it is sent with (or to) your webpage, and does things "on" your webpage.

php, however, is different. writing php as "part of" your webpage is limiting - it cuts you off from php's full power, almost always results in very sloppy code, and prevents you from really understanding how it works.

php happens on your server, before your webpage. not during, not after. php writes html code. ideally, all of your php should be separate from your html, and finish doing everything it will do first, with only one echo at the end: the completed html document.

james438
06-10-2011, 06:38 AM
Interesting thoughts. I was looking at a function I was given and confused that the function was defined after it was called, which is why I thought to ask about the correct procedure for coding functions, but I didn't think to ask.

This is enough to get me started on a project or two. Thanks for explaining. I plan to use your posts as a reference. I do wish that defining functions early did now waste resources however, otherwise it might be nice to define them all together early in the php file to make the function easy to locate.

traq
06-10-2011, 01:31 PM
Interesting thoughts. I was looking at a function I was given and confused that the function was defined after it was called, which is why I thought to ask about the correct procedure for coding functions, but I didn't think to ask

PHP parses everything first, and then executes the code. (that's why you get absolutely nothing when you have a syntax error, even if it's the last line of the script.) Functions, classes, etc. can be defined anywhere. Variables, constants, and so forth are set during execution, so they need to be set before they are used.

djr33
06-10-2011, 07:27 PM
Functions, classes, etc. can be defined anywhere.
As I explained in my post, this is a risky assumption: it does work most of the time, but they also can be parsed at run time, not when initially parsing the code as a whole. This occurs with includes, and when functions are defined in any sort of embedded context-- ifs, loops, etc.

traq
06-10-2011, 08:09 PM
yes - sorry for the oversight. an included script isn't parsed (obviously) until it is included.

djr33
06-10-2011, 08:35 PM
This made me think of an interesting complication. exit() and die() don't seem to stop functions from being loaded.


<?php
hello();
exit();
function hello() { echo 'hello'; }
?>
This works.

<?php
hello();
exit();
if (1==1) { function hello() { echo 'hello'; } }
?>
This doesn't work, as above.


I think this is a mistake in PHP. I don't see why functions should be parsed initially.