|
Posted by Jerry Stuckle on 04/17/06 23:38
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_Remittance()
>
> DHL.php
> DHL_Detected_EDI_Type()
> DHL_Parse_EDI_File()
> DHL_Create_Remittance()
>
> while (odbc_fetch_row($result) || $done) {
> $function = odbc_result($result, "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_Type
>
> 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($fileformat) {
switch($fileFormat) {
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.
jstucklex@attglobal.net
==================
[Back to original message]
|