|
Posted by Richard on 02/16/07 15:08
Erwin Moller
<since_humans_read_this_I_am_spammed_too_much@spamyourself.com> writes:
> 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.
I thought so.
> 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.
Macro substitution really. ok.
>
> 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.
ok, and straightforward enough. Nothing really special to PHP there.
>
> 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.
probably a good idea to seperate them to reduce load requirements i guess.
>
> 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.
fine.
>
> 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.
Simple directory question : the answer is obvious enough - yes. Create a
forms and a library directory and add them to the include path.
>
> 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.
to avoid the meed to call to set the include path in every file which
needs to include files. Am I confusing myself? Must the include path be
set in an ini file which is included in every page which makes use of
such includes?
>
> or in every file which references
>> includes?
>
> I do not understand that question. :-/
Hopefully clearer above :/
>
> 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
I will move to objects but am just getting a feel for php first and its
libraries etc.
>
> Hope that helps.
It has been a GREAT help, thanks.
>
> Regards,
> Erwin Moller
>
>
--
Navigation:
[Reply to this message]
|