473,383 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 8310
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
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...
15
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...
9
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...
11
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...
9
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...
14
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...
2
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...
28
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.