php 扩展类
我正在使用名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用神奇的 __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.
怎么样,在类的源文件中,重命名该类(很可能还有构造函数),然后使用类似的内容扩展该类
然后重命名该文件并使用旧名称创建一个新文件并包含重命名的文件。将扩展版本的代码放入新文件中。最终结果是,每个使用原始版本的文件都将看到新版本,而无需全部追踪。
唯一的主要问题是当该类的新版本可用时会发生什么。
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
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.
不幸的是,这是唯一的方法。您无法逆转继承。抱歉,祝你好运!
凯尔
Unfortunately, that would be the only way to do so. You cannot reverse inhertiance. Sorry and good luck!
Kyle
也许在这种情况下您不需要扩展该类。当您想要添加新功能或更改现有功能并保持原始类不变时,可以扩展类。通常这是要走的路。但是,如果您需要对现有类进行更改,然后更新对该类的所有引用以引用新类,为什么不简单地更改类代码本身呢?这样,引用就不必改变。
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.
另请查看工厂模式。通常,您应该将类创建和业务逻辑分开。
因此,当您遇到这样的问题时,您只需去更改工厂...
这样您就可以避免业务逻辑中的“新”构造,并使您的代码更易于管理
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...
that way you can avoid 'new' constructs in your business logic, and makes your code more manageable