多次加载PHP类

时间:2017-10-18 09:39:40

标签: php function class

我创建了一个具有多个私有公共函数和构造函数的类。它是连接到vCloud API的客户端。我想要两个对象加载此类的不同启动。它们必须并行存在。

$vcloud1 = new vCloud(0, 'system');
$vcloud2 = new vCloud(211, 'org');

当我检查$vcloud1的输出时,它加载了$vcloud2的信息。这是否正确,如果发生这种情况?知道如何多次加载一个类并隔离两个类加载吗?

这是我班级的一部分,它拥有最重要的功能。与用户和组织构建以登录。如果数据库中的信息存在,那么我们使用数据库信息进行身份验证,否则我们使用系统级凭据进行身份验证。所以我想有两个类加载,一个是用户级登录,另一个是系统级登录。

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;
    }
}

2 个答案:

答案 0 :(得分:0)

$vcloud1 = new vCloud('user1', 'system');
$vcloud2 = new vCloud('user2', 'org');

这足以使两个不相关的实例。

我想你的数据库返回了相同的结果。

答案 1 :(得分:0)

如何为每个检索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方法。