473,795 Members | 3,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with include and duplication function definitions

I have an application that uses several file formats for similar data.
So I've created various php files for each format containing the same
functions which produce the same end result.

Now I currently have a functions setup that just reads a file and
determines which file type it is and includes the correct source. My
goal is add a new function to each source called
My_File_Format( $filename) where each file checks to see if the
specified file is the format it knows how to work with.

The problem I'm running into is duplicate function names. Here is a
test case of what exactly I was attempting to do. Anyone know how to
actually get this to work?
Output:
Test1
Fatal error: Cannot redeclare test_case() (previously declared in
C:\Inetpub\Acco unting\test1.ph p:5) in C:\Inetpub\Acco unting\test2.ph p
on line 6

Assumed should be
Test1Test2

file testcase.php
<html>
<body>
<?php

class file_format
{
var $f;
function file_format($fi lename)
{
$this->f = $filename;
}
function is_format()
{
include "$this->f";
test_case();
}
}

$a = new file_format("te st1.php");
$b = new file_format("te st2.php");
$a->is_format();
$b->is_format();

return;
?>
</body>
</html>

file: test1.php
<?php

function test_case()
{
echo "Test1";
}
?>

file: test2.php
<?php

function test_case()
{
echo "Test2";
}
?>

Apr 14 '06 #1
14 3743
Wescotte wrote:
I have an application that uses several file formats for similar data.
So I've created various php files for each format containing the same
functions which produce the same end result.
<snip>

The problem I'm running into is duplicate function names. Here is a
test case of what exactly I was attempting to do. Anyone know how to
actually get this to work?

Output:
Test1
Fatal error: Cannot redeclare test_case() (previously declared in
C:\Inetpub\Acco unting\test1.ph p:5) in C:\Inetpub\Acco unting\test2.ph p
on line 6


Sorry Wescotte - while many people bemoan the lack of namespaces in PHP,
really this is just sloppy programming. If you are applying a procedural
methodology then you should be planning your program better - while you
might have deliberately chosen the same naming strategy for different the
functions in different include files - there's something wrong if it's
including more than one. In fairness, this is a very common mistake, and
one of the reasons for object oriented programming (which obviates such
eventualities).

So I guess the solutions are:
1) look at your flow of control - only include one file
2) or learn OO.

C.
Apr 14 '06 #2
The application is processing freight EDI files. We have mutliple
vendors who use various file formats. The goal was to create an easy
way for somebody else to add new vendors without having to mess with
the base code.

So they create a php file to with 3 functions

1. Check if the specified file is from that vendor
2. Parse the invoice data
3. Build remittance EDI files

So I'd have a table that would contain
Vendor # for our ERP/PHP file to include

If they wanted to add additional suport they would create a new PHP
file with the 3 functions I described and add an entry into the above
table

If you have a better suggestion on how to allow for such a task please
let me know
The current method is a global detect function that knows what files to
include, a global creation file that knows which files to include based
on detected type and a global remittance function that knows which file
to include based on the vendor #.

So if somebody wants to add a new vendor they have to append their
detection code and add code to all 3 functions. Granted it's only a few
lines each it's still something I'd prefer they didn't have to do.

Apr 14 '06 #3
Wescotte wrote:
The application is processing freight EDI files. We have mutliple
vendors who use various file formats. The goal was to create an easy
way for somebody else to add new vendors without having to mess with
the base code.

So they create a php file to with 3 functions

1. Check if the specified file is from that vendor
2. Parse the invoice data
3. Build remittance EDI files

So I'd have a table that would contain
Vendor # for our ERP/PHP file to include

If they wanted to add additional suport they would create a new PHP
file with the 3 functions I described and add an entry into the above
table

If you have a better suggestion on how to allow for such a task please
let me know
The current method is a global detect function that knows what files to
include, a global creation file that knows which files to include based
on detected type and a global remittance function that knows which file
to include based on the vendor #.

So if somebody wants to add a new vendor they have to append their
detection code and add code to all 3 functions. Granted it's only a few
lines each it's still something I'd prefer they didn't have to do.


Yes - learn OO programming.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 14 '06 #4
Can you be more specific? I believe I understand the OO programming and
how to use classes (and inheritance) but I fail ot see how I can allow
a class to have the same function name and yet completely different
code.

Apr 14 '06 #5
Wescotte wrote:
Can you be more specific? I believe I understand the OO programming and
how to use classes (and inheritance) but I fail ot see how I can allow
a class to have the same function name and yet completely different
code.


OK, stop thinking in functions. Think in objects.

Each type of file is a different type of object. Each will have it's own class
to do the processing. And every one of the classes can have the same function
names - but different code.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 14 '06 #6
Yes, I understand that but I still fail to see how to make it all work.

If I have a base edi file class and a class for each type that inherits
the base class that contains the 3 functions mentioned above I'd still
need do have something like

$query = "select class_name, class_source from class_list";
$result = odbc_exec($conn ection, $query);

$done = false;

while (odbc_fetch_row ($result) || $done) {
include odbc_result($re sult, "class_source") ;
$test_case = new odbc_result($re sult, "class_name "); // I can't
see this working but I haven't specifcally tested it yet
if ($test_case.Kno wn_File_Format( $fillename)) {
$test_case.Pars e_file($filenam e);
$done = true;
}
}
Again, the whole purpose of this was to allow a user to add additional
EDI file formats (and code to process them) without acessing any code.
Maybe I'm missing something obvious but I can't think my way around how
to actually get this to work without hard coding the source names/class
names.

Apr 17 '06 #7
Wescotte wrote:
Yes, I understand that but I still fail to see how to make it all work.

If I have a base edi file class and a class for each type that inherits
the base class that contains the 3 functions mentioned above I'd still
need do have something like

$query = "select class_name, class_source from class_list";
$result = odbc_exec($conn ection, $query);

$done = false;

while (odbc_fetch_row ($result) || $done) {
include odbc_result($re sult, "class_source") ;
$test_case = new odbc_result($re sult, "class_name "); // I can't
see this working but I haven't specifcally tested it yet
if ($test_case.Kno wn_File_Format( $fillename)) {
$test_case.Pars e_file($filenam e);
$done = true;
}
}
Again, the whole purpose of this was to allow a user to add additional
EDI file formats (and code to process them) without acessing any code.
Maybe I'm missing something obvious but I can't think my way around how
to actually get this to work without hard coding the source names/class
names.


No, you include all the class sources at the beginning. Then you instantiate
the appropriate one as you need it, i.e.

while (odbc_fetch_row ($result) || $done) {
// Get the file format here, then
$myObject = null;
switch($fileFor mat) {
case "format1" :
$myObject = new MyClass1();
break;
case "format2":
$myObject = new MyClass2();
break;
// etc.
}
if (myObject) {
$myObject->doSomething($o dbc_result); // Call function(s) in the class
}

Each class will handle one file format and all classes will have the same
function names. Just determine the file format, instantiate the appropriate
class and let it do the work.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 17 '06 #8
That is exactly what I did in my current version but I wanted a cleaner
method.

In the case you described above I would have to have to modify multiple
sectiosn of code to add a new class type instead of simply adding a new
row to a table that contains the code for a new type. I guess the only
method I can see would be again going by functions but having a prefix
to each say

Fedex.php
Fedex_Detected_ EDI_Type()
Fedex_Parse_EDI _File();
Fedex_Create_Re mittance()

DHL.php
DHL_Detected_ED I_Type()
DHL_Parse_EDI_F ile()
DHL_Create_Remi ttance()

while (odbc_fetch_row ($result) || $done) {
$function = odbc_result($re sult, "name") . "_Detected_EDI_ Type";
if ($function()) {
// Detected edi file format
// do something....
$done = true;
}
}

With this method I can create a table that contains the source code
filename
and the leading characters into the function name so that each name is
unique

Now to add a new type I simply add a new record ot the table rather
than adding a new case "formatX": instance in the detcting/pasing and
creating remittance sections
which allows me to isolate the code from the user yet allow them to
create code for new file types

While this method will work I was looking for a way to keep the
function names identically instead of having a leading unique string +
function name (Fedex or DHL) _Detected_EDI_T ype

Basically I was looking for the ability to include "code.php" and then
somehow uninclude it after I used it.

Apr 17 '06 #9
Wescotte wrote:
That is exactly what I did in my current version but I wanted a cleaner
method.

In the case you described above I would have to have to modify multiple
sectiosn of code to add a new class type instead of simply adding a new
row to a table that contains the code for a new type. I guess the only
method I can see would be again going by functions but having a prefix
to each say

Fedex.php
Fedex_Detected_ EDI_Type()
Fedex_Parse_EDI _File();
Fedex_Create_Re mittance()

DHL.php
DHL_Detected_ED I_Type()
DHL_Parse_EDI_F ile()
DHL_Create_Remi ttance()

while (odbc_fetch_row ($result) || $done) {
$function = odbc_result($re sult, "name") . "_Detected_EDI_ Type";
if ($function()) {
// Detected edi file format
// do something....
$done = true;
}
}

With this method I can create a table that contains the source code
filename
and the leading characters into the function name so that each name is
unique

Now to add a new type I simply add a new record ot the table rather
than adding a new case "formatX": instance in the detcting/pasing and
creating remittance sections
which allows me to isolate the code from the user yet allow them to
create code for new file types

While this method will work I was looking for a way to keep the
function names identically instead of having a leading unique string +
function name (Fedex or DHL) _Detected_EDI_T ype

Basically I was looking for the ability to include "code.php" and then
somehow uninclude it after I used it.


I don't see why you would need to do apply special naming when the
object will encapsulate that for you.

If you have a column in the DB table called something like 'class_name',
then I think you could do something like:

while( odbc_fetch_row( $result) || $done ) {
$class_name = odbc_result($re sult, 'class_name');
if( $obj = new $class_name() ) {
// call whatever method you need
$obj->Detected_EDI_T ype();
$obj->Parse_EDI_File ();
$obj->Create_Remitta nce();
}
}

-david-

Apr 17 '06 #10

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

Similar topics

9
2594
by: Daniel Moree | last post by:
I'm using MS VC++ 6.0 I'm working on a big project. I've currently have several files for this project. Here's the problem. I have one header file phead.h I have two code files main.cpp and gameloop.cpp phead.h has all my core declarations in it like my main globals. main.cpp has all my window initilization functions and my winproc loop.
3
1461
by: stanlo | last post by:
hi to everyone, this is still a follow up of my project ,mathematical expression.this project is meant to evaluate mathemtical expressions with oparators,+,-,*,/.more than two operands can be done, eg it should be able to do,1+9-45*7/12,or 4* 5+6*21.from reading and help from you i have been able to write the program.but my problem is to process the string of the input.i.e given for example an input string, 12+6*65/7, you know i have to...
10
1461
by: puzzlecracker | last post by:
can someone explain why int k; canNOT be definied in .h file? what are general rules for what can/cannot be placed in .h file. FAQ doesn't seem to address this problem.
12
2504
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int main()
14
6705
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping" process I am introducing namespaces to properly compartmentalise sections of the code into logical units. What I am speciffically trying to get right is the dependency tree for header files to reduce compile time and simplify the code structure. On...
1
5396
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started: Project: drawing_control, Configuration: Release Win32 ------ 1>Deleting intermediate and output files for project 'drawing_control', configuration 'Release|Win32'
2
3278
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
4
39432
by: desktop | last post by:
I have a main.cpp file that will call a single function. For the sake of curiousity I would like to place this function in a separate file called func.cpp and include it in my main.cpp with: #include "func.cpp". But it gives a multiple definitions error. Do I always have to make a .h file for a .cpp file if I want to include the functionality in another module?
4
1883
by: junyang | last post by:
Hi all, I have one DTD fragment, base.dtd, that contains a bunch of useful element definitions (say an element named "base"), and two DTD fragments, a.dtd and b.dtd, that each build on base.dtd and defines a few more elements. I use an entity to include base.dtd in a.dtd and b.dtd. For example, in a.dtd: <!ENTITY % include.base SYSTEM "base.dtd"> %include.base
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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...
1
7538
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
6779
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.