简体   繁体   English

什么是使用php在不同类中使用相同功能的最佳方法是什么

[英]what is the best approach to use same functions in different classes using php

i have a service class written in php which called different classes to do same functions. 我有一个用php编写的服务类,它调用不同的类来执行相同的功能。 In my each class, i am using the same functions for different clients. 在每堂课中,我为不同的客户使用相同的功能。 So every time a new client come to use my service, i just change the class name and the functions are remain same. 因此,每次有新客户使用我的服务时,我都只需更改类名,功能就保持不变。

I have been trying to use the factory method pattern. 我一直在尝试使用工厂方法模式。 But what i have figured out that if i use the factory method pattern, still i have to copy paste the same functions for different clients. 但是我发现,如果我使用工厂方法模式,仍然必须为不同的客户端复制粘贴相同的功能。

My scenario is i create a class to do the following. 我的情况是我创建一个类来执行以下操作。 For Client 1 functions are: setClient() process() 对于客户端1,函数为:setClient()process()

For Client 2 functions are: setClient() process() 对于Client 2,函数为:setClient()process()

I know i can use interface or abstract class. 我知道我可以使用接口或抽象类。 But still i feel there are some problems as the setClient() and process() functions do exactly the same things. 但是我仍然感到有些问题,因为setClient()和process()函数的作用完全相同。

So what is the best approach to handle these type of scenarios? 那么,处理此类情况的最佳方法是什么?

I don't want to write or copy paste the same functions again and again in different classes for different clients for the same purpose. 我不想为相同的目的在不同的类中一次又一次地在不同的类中编写或复制粘贴相同的函数。

advance thanks to all participants. 提前感谢所有参与者。

you can use an abstract class and then extend it in your client class. 您可以使用抽象类,然后在客户类中对其进行扩展。 lets define the abstract class first. 让我们先定义抽象类。

abstract class base {

     function common() {
           echo "common code here";
     } 
}

then inherit it for each client. 然后为每个客户端继承它。

class clienta extends base {

      function clientAfunc() {
          $this->common(); //call common
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM