473,804 Members | 5,054 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
14 3744
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.


The only code you would have to change would be that in the switch() statement -
and that could be an included file. Alternatively, you could do as David suggested.

Remember - the user won't be able to just add a new type. Someone's going to
have to write a class for that type, also. So along with adding the class, you
could just add the new type to the switch() statement.

Alternatively, you could place the switch statement in a static function in the
base class, i.e.

class BaseClass {
static function getType($filefo rmat) {
switch($fileFor mat) {
case "format1" :
return new MyClass1();
case "format2":
return new MyClass2();
// etc.
default:
return null;
}
}
}

Then you only need to change the base class.

Another alternative would be to have each class check to see what it can handle
via static functions, and ask it, i.e.

class DerivedClass1 {
static function CanIHandeThis ($fileFormat) {
return $fileFormat == 'FedEx';
}
}

But you would still need to have an array of the various types someplace (i.e. a
common base class). The advantage here is you've moved class-specific code to
the derived classes themselves. And you could still have the switch class in
your base class.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 17 '06 #11
Yeah, I didn't realize you could do new $class_name();

I think this will be the method I use

Apr 17 '06 #12
Sure, but I prefer to find a method that allows for virtually no
modification to the original code.

I do prefer to use the method David suggested. I wasn't aware I could
do something like $new_class = new $class_name_str ing;

I'd also like to thank you guys for all the help

Apr 17 '06 #13
Wescotte wrote:
Sure, but I prefer to find a method that allows for virtually no
modification to the original code.

I do prefer to use the method David suggested. I wasn't aware I could
do something like $new_class = new $class_name_str ing;

I'd also like to thank you guys for all the help


The only problem might be - I don't know if you can do it directly. You may
have to do something like

$myObject = eval(new $className);

Never tried it either way, though.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 17 '06 #14
Jerry Stuckle wrote:
Wescotte wrote:
Sure, but I prefer to find a method that allows for virtually no
modification to the original code.

I do prefer to use the method David suggested. I wasn't aware I could
do something like $new_class = new $class_name_str ing;

I'd also like to thank you guys for all the help


The only problem might be - I don't know if you can do it directly. You
may have to do something like

$myObject = eval(new $className);

Never tried it either way, though.

This seems to work as expected:

<?php
class A {
function ident() {
echo "This is class A\n";
}
}

class B {
function ident() {
echo "This is class B\n";
}
}

$classes = array('A', 'B');
foreach( $classes as $class ) {
$obj = new $class();
$obj->ident();
}
?>

-david-

Apr 17 '06 #15

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

Similar topics

9
2596
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
1462
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
2506
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
6708
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
5398
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
3279
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
39435
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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
10577
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...
0
10332
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...
1
10320
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
6853
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
5521
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...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.