This is a discussion on Accessing multiple same-named functions within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello, In my 'Main.php' I automatically include 3 different car company files (companies A, B and C) by some ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
In my 'Main.php' I automatically include 3 different car company files (companies A, B and C) by some filesearch function. A.php B.php C.php I want to calculate the lowest car price per company and display that company name. I want to add a new company simply by creating file 'D.php'. I want all included files to have the same structure. (like a database record) For each company I would like to execute a same-named function 'CalculateCarPrice()'. 'CalculateCarPrice()' function code/behaviour should be different for each manufacturer. How do I solve this problem in PHP? How do I create many same-named functions and store a reference to these functions in some array. Or is this impossible? (It's not allowed to declare same-named class-names or same-named function-names in included files more than once, of course) Regards, Henk |
|
|||
|
Basically you should use object-oriented PHP. Define one class in let's
say cars.class.php as: class Cars { var $company; function Cars($company) { $this->company=$company; } function CalculateCarPrice() { switch ($this->company) { case 'A': //calculate car price here break; case 'B': //calculate car price here break; //etc... } return $price; } } Then you can create your files as : //A.php require_once('cars.class.php'); $car=new Cars('A'); echo $car->CalculateCarPrice(); i hope this helps. Regards, Ivan Pavlov |
|
|||
|
Henk van Winkoop wrote:
> Hello, > > In my 'Main.php' I automatically include 3 different car company files > (companies A, B and C) by some filesearch function. > A.php > B.php > C.php > > I want to calculate the lowest car price per company and display that > company name. > I want to add a new company simply by creating file 'D.php'. > You're barking up the wrong tree here. While (as Ivan suggested) you could use derived classes, or you could implement the different companies as differetn URLS and use file_get_contents() (or similar) to retrieve the results, a proper solution would describe the differences in data and use the same code to process the request. C. |