php 扩展类

发布于 2024-08-05 18:52:26 字数 490 浏览 8 评论 0原文

我正在使用名为 SforceEnterpriseClient 的 salesforce 类。我在我的应用程序中的很多地方都引用了该类。我想扩展该类,使其能够从 1 行记录集返回单个数组,现在记录集的深度约为 3 层。我还想用它做一些其他的事情。我可以处理这一切。

我读到的有关类的所有内容都表明,当我扩展一个类时,我需要这样调用新的类:

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

这意味着我必须在所有现有代码中查找/替换。 是否可以用子项覆盖父项,这样我就不必玩查找/替换游戏了?

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

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 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

单身狗的梦 2024-08-12 18:52:26

您可以使用神奇的 __autoload() 函数玩一些类加载技巧并删除对 salesforce 文件的引用,即。要求,要求_一次,包括,包括_一次;但为了可读性和可维护性,您可能应该在此处采取较长的路线并修改所有引用以使用子类。

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.

热鲨 2024-08-12 18:52:26

怎么样,在类的源文件中,重命名该类(很可能还有构造函数),然后使用类似的内容扩展该类

class SforceEnterpriseClient extends renamedClass {};

然后重命名该文件并使用旧名称创建一个新文件并包含重命名的文件。将扩展版本的代码放入新文件中。最终结果是,每个使用原始版本的文件都将看到新版本,而无需全部追踪。

唯一的主要问题是当该类的新版本可用时会发生什么。

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.

寂寞陪衬 2024-08-12 18:52:26

不幸的是,这是唯一的方法。您无法逆转继承。抱歉,祝你好运!

凯尔

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

Kyle

北方的韩爷 2024-08-12 18:52:26

也许在这种情况下您不需要扩展该类。当您想要添加新功能或更改现有功能并保持原始类不变时,可以扩展类。通常这是要走的路。但是,如果您需要对现有类进行更改,然后更新对该类的所有引用以引用新类,为什么不简单地更改类代码本身呢?这样,引用就不必改变。

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.

无名指的心愿 2024-08-12 18:52:26

另请查看工厂模式。通常,您应该将类​​创建和业务逻辑分开。
因此,当您遇到这样的问题时,您只需去更改工厂...

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

这样您就可以避免业​​务逻辑中的“新”构造,并使您的代码更易于管理

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文