473,915 Members | 4,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8358
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.ph p" as an external program and call it - but that's a lot of
extra effort.

If all the functions and data in "myfunc1.ph p" 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*******@attgl obal.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_do nt" 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_do nt" 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*******@attgl obal.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.p hp');
include('LibB.p hp');

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('p arse.php');
fileb.php:
include_once('p arse.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*******@attgl obal.net
=============== ===
Sep 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4640
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
6
5844
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
3988
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
15
6611
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
9
6374
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
11
3860
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
9
2190
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and variables?
14
6048
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
2
3784
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
28
8901
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and didn't find any good explanation about it. Could the experts please give me an explanation or point me to some link? Thanks! /Why Tea
0
10923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4778
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.