views:

73

answers:

5

I'm using a salesforce class called SforceEnterpriseClient. I've referenced that class many places in my application. I want to extend that class to give it the ability to return a single array from a 1 row recordset, right now the record set is about 3 levels deep. There's a few other things I want to do with it as well. I can handle all that.

Everything I've read about classes says that when I extend a class, I need to call the new one as such:

class MySF extends SforceEnterpriseClient {};
$mySforceConnection = new $MySF;

That means in all of my existing code I have to find/replace. Is it possible to overwrite the parent with the child so I don't have to play the find/replace game?

class SforceEnterpriseClient  extends SforceEnterpriseClient {};
$mySforceConnection = new $SforceEnterpriseClient ;
A: 

Unfortunately, that would be the only way to do so. You cannot reverse inhertiance. Sorry and good luck!

Kyle

Kyle J. Dye
+1  A: 

You can probably play some classloading tricks with the magic __autoload() function and removing references to the salesforce file ie. require, require_once, include, include_once; But in the interest of readability and maintainability, you should probably take the long route here and modify all your references to use the subclass.

Asaph
A: 

Maybe you do not need to extend the class in this scenario. You extend a class when you want to add new functionality or change existing functionality AND keep the original class intact. Usually this is the way to go. But, if you need to make a change to an existing class and then update all references to the class to refer to the new class why not simply change the class code itself? That way the references would not have to change.

Vincent Ramdhanie
A: 

Also have a look at the factory pattern. Normally you should keep class creation and business logic separate.
So when you come across a problem like this, you only have to go and change the factory...

$sfEnterpriseClient = Factory::getSFEnterpriseClient($params);

that way you can avoid 'new' constructs in your business logic, and makes your code more manageable

Nicky De Maeyer
A: 

How about this, in the source file for the class, rename the class (and most likely the constructor as well) then extend the class using something like

class SforceEnterpriseClient extends renamedClass {};

Then rename the file and create a new file with the old name and include the renamed file. Put the code for your extended version in the new file. The final result is that every file that was using the original will see the new version without having to track them all down.

About the only major issue would be what happens when a new version of the class becomes available.

Tom
Great idea. I was trying to avoid altering the class itself so when a new version comes around I don't have to worry about putting my changes into it. But I can handle renaming each time there's a new version.Perfect, thanks!