469,312 Members | 2,503 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Are there static functions in PHP?

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?

Thanks!
Pete
Sep 10 '05 #1
11 8101
Do both parseArguments() functions do the same thing?

Sep 10 '05 #2
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?

Thanks!
Pete


Pete,

No, there aren't any static functions like in C because there isn't the
concept of separate modules like in C.

The C equivalent would be:
#include "myfunc1.c"
$include "myfunc2.c"

which would suffer from the same problem. But in C you can compile
"myfunc1" and "myfunc2" into different modules and have different static
functions. Unfortunately, that feature isn't available in C++.
Probably the best you could do to emulate it would be to set up
"myfunc1.php" as an external program and call it - but that's a lot of
extra effort.

If all the functions and data in "myfunc1.php" are related, you could
create a class. The same with 'myfunc2.php". Different classes can
have the same function name. If a class isn't applicable, you'll have
to change the function names so each is unique.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '05 #3
On Sat, 10 Sep 2005 16:35:05 +0000, Peter Salzman wrote:

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.


What you described is called "private method".

class A {

public function now_you_see_me() {...}
private function now_you_dont() {}
}

Function "now_you_dont" will not be visible from any method which is not
a member function of the class A. That is available as of PHP5.

--
http://www.mgogala.com

Sep 11 '05 #4
Mladen Gogala wrote:
On Sat, 10 Sep 2005 16:35:05 +0000, Peter Salzman wrote:

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.

What you described is called "private method".

class A {

public function now_you_see_me() {...}
private function now_you_dont() {}
}

Function "now_you_dont" will not be visible from any method which is not
a member function of the class A. That is available as of PHP5.


Mladen,

No, private methods are different. C doesn't have classes, so it can't
have class members.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '05 #5
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
Sep 11 '05 #6
On Sat, 10 Sep 2005 20:25:55 -0500, Jerry Stuckle wrote:
Mladen,

No, private methods are different. C doesn't have classes, so it can't
have class members.


Jerry, of course C doesn't have classes. C isn't an OO language while PHP
is. Also, strictly speaking, there is no linking of PHP scripts, so the
notion of a symbol local for an object file is not easy to translate to
PHP. The best thing to do, if he wants to hide functions from the outside
world, is to use private methods. Yes, that implies classes. PHP and
C are different to that extent that you can't really compare them feature
by feature so we have to think outside the box and come up with the best
functional match. And that is a private method.

--
http://www.mgogala.com

Sep 11 '05 #7
This is the way I would go about doing this:
filea.php:
include_once('parse.php');
fileb.php:
include_once('parse.php');
parse.php:
function parseArguments(){...}

php can detect if the file parse.php is already included and not
include it again, hence not redeclaring the function. Give that a go.

Sep 11 '05 #8
On 11 Sep 2005 01:38:50 -0700, cyberhorse wrote:
This is the way I would go about doing this: <snip> include it again, hence not redeclaring the function. Give that a go.


Who are you talking to and what are you talking about? Learn to Quote!
Sep 11 '05 #9
Mladen Gogala wrote:
On Sat, 10 Sep 2005 20:25:55 -0500, Jerry Stuckle wrote:

Mladen,

No, private methods are different. C doesn't have classes, so it can't
have class members.

Jerry, of course C doesn't have classes. C isn't an OO language while PHP
is. Also, strictly speaking, there is no linking of PHP scripts, so the
notion of a symbol local for an object file is not easy to translate to
PHP. The best thing to do, if he wants to hide functions from the outside
world, is to use private methods. Yes, that implies classes. PHP and
C are different to that extent that you can't really compare them feature
by feature so we have to think outside the box and come up with the best
functional match. And that is a private method.


Mladen,

But if you're going to use classes, you don't need to use private methods.

The problem he's trying to solve is namespace collision. He has two
different functions with the same name and occasionally he needs to
include both in a third file. In C this isn't a problem because you can
use static functions.

In PHP you don't need to use private methods - and may not want to use
them, since they can't be called from outside the class. Just having a
method as a member of the class is sufficient to resolve the namespace
collision. And it can still be called by non-members.

Of course, this really isn't different than having different function
names in the first place - which may still be the easiest way to go.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '05 #10
cyberhorse wrote:
This is the way I would go about doing this:
filea.php:
include_once('parse.php');
fileb.php:
include_once('parse.php');
parse.php:
function parseArguments(){...}

php can detect if the file parse.php is already included and not
include it again, hence not redeclaring the function. Give that a go.


Read again. This won't help as he has "parseArguments" in both
filea.php and fileb.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '05 #11
Thanks for all the great suggestions!

I've re-implemented my PHP "modules" as classes, and it's saved me from
the namespace clashes. It's a great solution!

I do wish that PHP were a bit more flexible with setting linkage on objects.
In general, I love PHP. But there are small things here and there that I
really wish were different (I keep wanting to call my arrays @myArray, for
example). ;)

Pete
Sep 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dumitru Sipos | last post: by
1 post views Thread by Bryan Parkoff | last post: by
15 posts views Thread by Samee Zahur | last post: by
9 posts views Thread by Bryan Parkoff | last post: by
9 posts views Thread by Pohihihi | last post: by
14 posts views Thread by Jess | last post: by
2 posts views Thread by Nagrik | last post: by
28 posts views Thread by Why Tea | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.