我创建了一个类,它有多个私有函数、一个公共函数和一个构造函数。它是一个连接到vCloud应用程序接口的客户端。我想要两个对象加载这个类的不同初始化。它们必须并行存在。
$vcloud1 = new vCloud(0, 'system');
$vcloud2 = new vCloud(211, 'org');当我检查$vcloud1的输出时,它加载了$vcloud2的信息。这是正确的吗,应该发生这种情况吗?你知道如何多次加载一个类并隔离两个类的加载吗?
这是我的类的一部分,它拥有最重要的功能。使用要登录的用户和组织构造。如果数据库中存在info,则使用DB info进行身份验证,否则使用系统级凭据进行身份验证。因此,我希望有两个类加载,一个是用户级登录,另一个是系统级登录。
class vCloud {
    private $client;
    private $session_id;
    private $sdk_ver = '7.0'; 
    private $system_user = 'xxxxxxxxxxx';
    private $system_password = 'xxxxxxxxxxxxxxxxx';
    private $system_host = 'xxxxxxxxxxxxx';
    private $org_user;
    private $org_password;
    private $org_host;
    private $base_url;
    public function __construct($customerId, $orgName) {
        if ($this->vcloud_get_db_info($customerId)) {
            $this->base_url = 'https://' . $this->org_host . '/api/';
            $this->base_user = $this->org_user . "@" . $orgName;
            $this->base_password = $this->org_password;
        } else {
            $this->base_url = 'https://' . $this->system_host . '/api/';
            $this->base_user = $this->system_user;
            $this->base_password = $this->system_password;
        }
        $response = \Httpful\Request::post($this->base_url . 'sessions')
            ->addHeaders([
                'Accept' => 'application/*+xml;version=' . $this->sdk_ver
            ])
            ->authenticateWith($this->base_user, $this->base_password)
            ->send();
        $this->client = Httpful\Request::init()
            ->addHeaders([
                'Accept' => 'application/*+xml;version=' . $this->sdk_ver,
                'x-vcloud-authorization' => $response->headers['x-vcloud-authorization']
            ]);
        Httpful\Request::ini($this->client);
    }
    public function __destruct() {
        $deleted = $this->vcloud_delete_session();
        if (!$deleted) {
            echo "vCloud API session could not be deleted. Contact administrator if you see this message.";
        }
    }
    private function vcloud_delete_session() {
        if (isset($this->client)) {
            $response = $this->client::delete($this->base_url . 'session')->send();
            return $response->code == 204;
        } else {
            return FALSE;
        }
    }
    public function vcloud_get_db_info($customerId) {
        global $db_handle;
        $result = $db_handle->runQuery("SELECT * from vdc WHERE customer=" . $customerId);
        if ($result) {
            foreach ($result as $row) {
                if ($row['org_host'] != "") {
                    $this->org_user = $row['org_user'];
                    $this->org_password = $row['org_password'];
                    $this->org_host = $row['org_host'];
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }
    public function vcloud_get_admin_orgs() {
        $response = $this->client::get($this->base_url . 'query?type=organization&sortAsc=name&pageSize=100')->send();
        return $response->body;
    }
}发布于 2017-10-18 18:05:20
$vcloud1 = new vCloud('user1', 'system');
$vcloud2 = new vCloud('user2', 'org');这足以创建两个不相关的实例。
我认为您的数据库返回的是相同的结果。
发布于 2017-10-18 18:08:55
为每个检索vCloud实例的对象提供一个自定义的equals方法怎么样?
class vCloud {
  //Other definitions
  public function equals(vCloud $other){
    //Return true if $other is same as this class (has same client_id etc etc)
  }
}所以你只需要按照代码所说的去做:
$vcloud1 = new vCloud('user1', 'system');
$vcloud2 = new vCloud('user2', 'org');
if($vcloud1.equals($vclous2)){
 echo "Entries are the same";
} else {
 echo "Entries are NOT the same";
}此外,您可能需要在类定义中包含各种getter和setter方法。您需要做的是填充equals方法。
https://stackoverflow.com/questions/46807137
复制相似问题