By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,633 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,633 IT Pros & Developers. It's quick & easy.

include files with similar contents

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.