You are here: Re: Functions v includes « PHP Programming Language « IT news, forums, messages
Re: Functions v includes

Posted by Erwin Moller on 02/16/07 14:14

Richard wrote:

>
> I am new to php and am looking for some quick "get up and go" help.
>
> What would the arguments for and against be for function declarations v
> simple include?
>
> e.g
>
> <?php
> include("myfunc.php"); // declares a function "myfunc".
> myfunc("hello");
> ?>
>
> and
>
> <?php
> include("myfunc.php"); // just a bunch of php code with no
> function
> ?>
>
> Does PHP cache the functions?

Nope.
Every request to a certain page will cause the script to be executed again.
Maybe some smart caching is going on behind the scenes, not sure.
Most FILESYSTEMS however are able to cache files in memory, so the disk-IO
is eliminated needed to get the filecontant of your PHP scripts.

This happens behind your back, so you don't need to enforce it somehow (that
is: on every system I saw)

Must functions be included on all pages
> which use them?

Yes.
PHP doesn't care if your function is made available to PHP via a direct
declaration in your script, or that the function is included.
You might best think of includes as being put right into place into the
mainscript, as if you putted them there.

The plethora of web resources makes some of this a
> trifle confusing and difficult/time consuming to pin down, so any best
> practice pointers much appreciated.

My best practise goes like this:
In every script include first a small general file that sets some important
settings. I name such file often ini_settings.php.
It contains stuff like:
- how are session handled (Database or flatfile (default) or memory)
- What is the include_path?
- an errorhandler
etc. That kind of stuff.

And I have (sometimes multiple) function files, boringly named
functions.php, that contains all functions I use in a certain project.
If you have a lot of functions, it might make sense to break them up
logically in different files. Up to you and your requirements.

Then from php:
require_once("../ini_settings.php"); // <-- That path is coded relatively
require_one("myDateFunctions.php"); // Will be include based on include_path
as defined in ini_settings.php.

One a sidenote:
I was/am never much of a speedfreak, and prefer having clean code above fast
code, so I don't mind putting all functions in one big includefile that is
included everewhere.
In most cases: If your app is slow, the reason is not a large includefile,
but bad database-design/repetitive queries/etc.

>
> Would you keep functions & includes seperate from the main pages?

What do you mean by that?
All pages that need functionality that is in your includefile should have
access to it of course.

is it
> then necessary to specify a fully qualified name when including them or
> should one just set the include_path in a header?

You don't set the includepath in the header (not the http-header).
I think you mean 'above the rest of the script'.

I like setting the includepath, because it safes me the headache of thinking
up the relative path to the file. And using the absolute path has the
irritating side effect that moving your app forces you to check them all.
So keep the include_path in one place, for ease of use and migrating. (In my
example it is defined in ini_settings.php)

Can the include path
> just be set once for a single session?

Yes, if you want. Store the path in the $_SESSION.
I don't see the advantage, but I don't know your application of course.

or in every file which references
> includes?

I do not understand that question. :-/

Can you recommend a web resource which covers this type of
> stuff?

All the above, which was just published on this great resource:
comp.lang.php.
;-)

On a sidenote: If you feel comfortable with OO, you can also design your
application with objects. This leads to a very different kind of
programming, but also let you design in a neat way. It is a matter of taste
what you prefer. Personally I like both. :P

Hope that helps.

Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация