|
Posted by Bent Stigsen on 10/03/83 11:26
Peter Salzman wrote:
> I have two files which implement functionality in many of my web pages. Each
> file uses a function named "parseArguments()" that's critical for each of the
> two files.
>
> I often include both files into one webpage, which results in a name clash
> for parseArguments().
>
> What I really would like is the concept of static functions from the C
> programming language. A static function is one that has strict file scope:
> no function outisde the containing module sees the function.
>
> Is there a way to limit the "linkage" of a function to file scope only?
Somewhat. As Jerry mentions, you could define them within a class.
Sort of a cheap namespace. The "only" nuisance is that you have to
prefix function-calls with the classname.
For example:
[LibA.php]
<?php
class LibA {
function foo() {
echo __CLASS__, '::', __FUNCTION__, "\n";
}
function bar() {
echo __CLASS__, '::', __FUNCTION__, ' -> ';
LibA::foo();
}
}
?>
[LibB.php]
<?php
class LibB {
function foo() {
echo __CLASS__, '::', __FUNCTION__, "\n";
}
}
?>
[main.php]
<?php
include('LibA.php');
include('LibB.php');
LibA::foo();
LibA::bar();
LibB::foo();
?>
For "local" variables, you could have an array for each file, or if
you have PHP5, you can make them static within the classes. But if you
get a nameclash between class/file-names, then you are back where you
started.
To rant a bit and paraphrase a teacher I once had. Use sensible
naming-conventions for variables, functions, classes, files, etc. and
enforce it vigorously. Names should never be vague or ambiguous, and
preferably readable and memorable. (Except when giving examples, I think)
/Bent
Navigation:
[Reply to this message]
|