473,326 Members | 2,061 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,326 software developers and data experts.

include files with similar contents

Hi.
Another question for you guys, you are very helpful.
If I have the files a.php, b.php, c.php and I must include them, but they
all contain the same function foo(), is there a way to tell PHP to call the
function foo from a.php, foo from b.php , and so on? Or will the last
included file replace the previous foo() functions?

Any suggestion on a workaround? Can I call afile.php which inclueds a.php
and runs foo() from a.php?
Or will I have to rewrite all the files a.php, b.php, etc. ?
--
I didn't know sci.bio.paleontology was that low traffic until I tried read
the thread "Where is everyone?" and found it to be expired.
Feb 5 '07 #1
12 1250
Rik
Gunnar G <de****@comhem.sewrote:
Hi.
Another question for you guys, you are very helpful.
If I have the files a.php, b.php, c.php and I must include them, but they
all contain the same function foo(), is there a way to tell PHP to call
the
function foo from a.php, foo from b.php , and so on? Or will the last
included file replace the previous foo() functions?

Any suggestion on a workaround? Can I call afile.php which inclueds
a.php
and runs foo() from a.php?
Or will I have to rewrite all the files a.php, b.php, etc. ?
A function cannot be redeclared, and this will result in an error. If they
do different things, you should name them different accordingly. You can
however declare functions in mulitple places with a conditional, allthough
it's not advisable (if the function changes you have to make changes all
over). This is done by:
if(!function_exists('foo')){
function foo(){
//etc...
}
}
--
Rik Wasmus
Feb 5 '07 #2
A function cannot be redeclared, and this will result in an error. If they
do different things, you should name them different accordingly. You can
however declare functions in mulitple places with a conditional, allthough
it's not advisable (if the function changes you have to make changes all
over). This is done by:
if(!function_exists('foo')){
function foo(){
//etc...
}
}
This makes me think.

Imagine you enter the file mypage.php that contains a function foo, and you
have to run the function foo that is in the file a.php.
Then you can't include a.php, so then how do execute the code in a.php
withouth leaving mypage.php? a.php should return one or two values back
to mypage.php.

I'd rather not rename the functions, so is there any solution to this?
Feb 5 '07 #3
Gunnar G wrote:
>A function cannot be redeclared, and this will result in an error. If they
do different things, you should name them different accordingly. You can
however declare functions in mulitple places with a conditional, allthough
it's not advisable (if the function changes you have to make changes all
over). This is done by:
if(!function_exists('foo')){
function foo(){
//etc...
}
}

This makes me think.

Imagine you enter the file mypage.php that contains a function foo, and you
have to run the function foo that is in the file a.php.
Then you can't include a.php, so then how do execute the code in a.php
withouth leaving mypage.php? a.php should return one or two values back
to mypage.php.

I'd rather not rename the functions, so is there any solution to this?
Gunnar,

You don't. As Rik said - different functions should have different names.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 5 '07 #4
You don't. As Rik said - different functions should have different names.
Dang.
A lot of the stuff is automatically generated so then it will be a little
problematic if I have to rename the files.
Feb 5 '07 #5
Gunnar G wrote:
>You don't. As Rik said - different functions should have different names.
Dang.
A lot of the stuff is automatically generated so then it will be a little
problematic if I have to rename the files.
In that case your generator should have some means of adding a prefix or
suffix similar to the function name to make it unique.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 6 '07 #6
You can use classes :
<?php
class a
{
include "a.php";
}
class b
{
include "b.php";
}

a::foo();
b::foo();
?>

http://www.mastervb.net/phpbooks/
http://www.mastervb.net
On Feb 6, 3:56 am, Gunnar G <deb...@comhem.sewrote:
Hi.
Another question for you guys, you are very helpful.
If I have the files a.php, b.php, c.php and I must include them, but they
all contain the same function foo(), is there a way to tell PHP to call the
function foo from a.php, foo from b.php , and so on? Or will the last
included file replace the previous foo() functions?

Any suggestion on a workaround? Can I call afile.php which inclueds a.php
and runs foo() from a.php?
Or will I have to rewrite all the files a.php, b.php, etc. ?
--
I didn't know sci.bio.paleontology was that low traffic until I tried read
the thread "Where is everyone?" and found it to be expired.

Feb 6 '07 #7
Rik
Gunnar G <de****@comhem.sewrote:
>You don't. As Rik said - different functions should have different
names.
Dang.
A lot of the stuff is automatically generated so then it will be a little
problematic if I have to rename the files.
If it's truly created on the fly, use create_function() for an anonymous
function identifier.
--
Rik Wasmus*
Feb 6 '07 #8
You can use classes :
<?php
class a
{
include "a.php";
}
class b
{
include "b.php";
}

a::foo();
b::foo();
?>

http://www.mastervb.net/phpbooks/
http://www.mastervb.net
Thanks, that was the best answer yet. I will try that. So in a sence, there
are namespaces, if you use classes. Great! :-)
Feb 6 '07 #9
>You can use classes :
><?php
class a
{
include "a.php";
}
class b
{
include "b.php";
}

a::foo();
b::foo();
?>
Wait a minute, are you sure this works?
I try
include("page.php");
class a
{
function foo($x){return $x+1;}
}

class b
{
function foo($x){return $x+1;}
}

which works, but moving the include line to this

class a
{
include("page.php");
function foo($x){return $x+1;}
}

class b
{
function foo($x){return $x+1;}
}

and it no longer works.

Feb 6 '07 #10
"Gunnar G" <de****@comhem.sewrote in message
news:Lm*******************@newsb.telia.net...
... but moving the include line to this

class a
{
include("page.php");
function foo($x){return $x+1;}
}

class b
{
function foo($x){return $x+1;}
}

and it no longer works.

I'm guessing page.php defines another function named foo. In this case, just
take the extra foo from class a and throw it out the window. If you include
it from page.php, you don't need top redefine it... doi!

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 6 '07 #11
Kimmo Laine wrote:
"Gunnar G" <de****@comhem.sewrote in message
news:Lm*******************@newsb.telia.net...
>... but moving the include line to this

class a
{
include("page.php");
function foo($x){return $x+1;}
}

class b
{
function foo($x){return $x+1;}
}

and it no longer works.


I'm guessing page.php defines another function named foo. In this case,
just take the extra foo from class a and throw it out the window. If you
include it from page.php, you don't need top redefine it... doi!
Well, in this case page.php had only 6 characters in it

<?php ?>

so there should'nt be any problem.Very strange.
Feb 6 '07 #12
"Gunnar G" <de****@comhem.sewrote in message
news:qg*******************@newsb.telia.net...
Kimmo Laine wrote:
>"Gunnar G" <de****@comhem.sewrote in message
news:Lm*******************@newsb.telia.net...
>>... but moving the include line to this

class a
{
include("page.php");
function foo($x){return $x+1;}
}

class b
{
function foo($x){return $x+1;}
}

and it no longer works.


I'm guessing page.php defines another function named foo. In this case,
just take the extra foo from class a and throw it out the window. If you
include it from page.php, you don't need top redefine it... doi!
Well, in this case page.php had only 6 characters in it

<?php ?>

so there should'nt be any problem.Very strange.
Okay, wasn't thinking straight. You can't call a function inside a class
definition. If you were inside a class method, then again it would be okay.
So you want:

class a {
function foo($x){
include("page.php");
return $x+1;
}
}
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 6 '07 #13

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

Similar topics

2
by: Marcus | last post by:
Hello, I am having problems with an include statement. I'm setting a session variable flag and then including a file, and in that include file I have a check at the top to make sure that the...
1
by: Kris | last post by:
Question: How do you create an Installer program using the Package and Deployment Wizard provided by Visual Studio Pro 6.0 (SP5) to include subfolders and their contents. I understand how to...
10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
2
by: Garry Freemyer | last post by:
I wrote a screensaver, via Visual Studio 2003 in C# and I decided a wiser choice for me was to use an xml file to save my configs. Btw: I wonder if this is why every bit of documentation I've found...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
5
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h"...
26
by: Jan Engelhardt | last post by:
Hello, assume foo/bar.h exists, and is included as '#include "bar.h"' in foo/bar.c. `gcc -c foo/bar.c` will compile it fine (i.e. finds the file). Is this a gcc extension or a C compiler...
12
by: FFMG | last post by:
Hi, Please help me settle a geek argument :). I believe that php caches included code whenever possible. So given the 2 files bellow. // file.php -------------------------------------...
3
by: clinisbut | last post by:
may be i'm asking a stupid question, but, is it possible to include a header file at runtime?? I have some header files with this inside: {{12, 16},{ 0x00,0x00, /* ................ */...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.