简体   繁体   English

PHP OOP-在其他类中使用对象

[英]PHP OOP - using an object inside other classes

I've been a php programmer for a number of years, but am only now getting into OOP. 我已经是一名PHP程序员很多年了,但是直到现在才进入OOP。 I have two classes so far, Item and List (I'm just simplifying here, they're not the real class names.) All of my data is accessed via a web service using SOAP. 到目前为止,我有两个类,即Item和List(我只是在这里简化,它们不是真正的类名。)我的所有数据都是通过使用SOAP的Web服务访问的。

I'm not sure what the best way is to implement getting a SOAP client and using it in multiple classes. 我不确定哪种最佳方法是实现获取SOAP客户端并在多个类中使用它。 For example, the WSDL defines an addItem function and a getList function. 例如,WSDL定义了addItem函数和getList函数。 I also need to send a token to the service for each transaction. 我还需要为每笔交易向服务发送令牌。

Do I need to define a new SoapClient inside every class? 我是否需要在每个类中定义一个新的SoapClient? like so: 像这样:

class Item {
  // item properties
  function addItem($itemName) {
     $client = new SoapClient('myfile.wsdl');
     $client->addItem($itemName);
     //service returns true or false
  }
}

class List {
  function getList($listName) {
     $client = new SoapClient('myfile.wsdl');
     return $client->getList($listName);
     //service returns array
  }
}

Or is there some way to create a new SoapClient outside of the individual classes and use the same client object in each one? 还是有某种方法可以在各个类之外创建新的SoapClient并在每个类中使用相同的客户端对象?

Pass it into the constructor with some dependency injection: 通过一些依赖注入将其传递给构造函数:

class Item {

  function __constructor(SoapClient &$s){
      $this->soap = $s;
  }
  // item properties
  function addItem($itemName) {
     $this->soap->addItem($itemName);
     //service returns true or false
  }
}

Depending on how you're using the SoapClient, you have a couple of options. 根据您使用SoapClient的方式,有两种选择。

If you're going to use that SoapClient multiple times during the execution of the script (and it is thread safe?), you could use the singleton pattern to fetch an instance of the SoapClient. 如果您打算在脚本执行期间多次使用该SoapClient(并且它是线程安全的吗?),则可以使用单例模式来获取SoapClient的实例。 What this would allow you to do is to only create ONE instance of the object and then fetch it again each time you need it. 这将使您只能创建对象的一个​​实例,然后在每次需要时再次获取它。

To fetch the object the code would look like. 为了获取对象,代码看起来像。

$soapclass = $SoapClassSingleton::getInstance()

And the class behind would look something like 后面的课看起来像

class SoapClassSingleton{
private instance;
public static function getInstance(){
    if(!self::instance){            
        self::$instance = new SoapClass();          
    }
    return self::$instance;
}

It's almost like having a "global" variable, but is only created if you need it and then the same object can be used by other classes. 这几乎就像具有“全局”变量一样,但是仅在需要时才创建,然后其他类可以使用同一对象。 I use this design for logging in my applications. 我使用这种设计来登录我的应用程序。

You could also create an instance of the SoapClient and pass it into the the Class when you create it. 您还可以创建SoapClient的实例,并在创建它时将其传递给Class。 See PHP's constructors for help. 请参阅PHP的构造函数以获取帮助。

Alternatively your classes could extend a class that contains the SoapController. 或者,您的类可以扩展包含SoapController的类。 The downside to doing this is that now you are tightly coupled with the superclass. 这样做的缺点是现在您已经与超类紧密结合。

Oh dear, there are so many possible ways to do this. 亲爱的,有很多方法可以做到这一点。 If you want to keep it the exact SAME in ALL portions of the script... you could use a singleton class. 如果要在脚本的所有部分中保持相同的名称,则可以使用单例类。

Access: 访问:

MySoapConnection::instance()->function_calls_here

Class: 类:

class MySoapConnection {

  /* Singleton */
  private static $_instance;
  public static function instance()
  {
    if (! self::$_instance):
      self::$_instance = new self();
    endif;
    return self::$_instance;
  }


  function __constructor(SoapClient $s){
      $this->soap = $s;
  }
  // item properties
  function addItem($itemName) {
     $this->soap->addItem($itemName);
     //service returns true or false
  }
}

As the comments state, I haven't tested. 如评论所述,我尚未测试。 Nor should you copy/paste this code. 您也不应复制/粘贴此代码。 Just shows a Singleton example. 仅显示一个Singleton示例。 Nothing more, Nothing less. 没事,没事。 PHP was not built for OOP, I love PHP, was adapted to use OOP, not built for it. PHP不是为OOP构建的,我很喜欢PHP,它被适配为使用OOP,而不是为此而构建的。

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

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