Accessing multiple same-named functions

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-27-2005
Henk van Winkoop
 
Posts: n/a
Default Accessing multiple same-named functions

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


Reply With Quote
  #2 (permalink)  
Old 09-27-2005
ivan.pavlov@gmail.com
 
Posts: n/a
Default Re: Accessing multiple same-named functions

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

Reply With Quote
  #3 (permalink)  
Old 09-28-2005
Colin McKinnon
 
Posts: n/a
Default Re: Accessing multiple same-named functions

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:43 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0