有没有更干净的创建数组的方法?

时间:2019-01-16 13:51:00

标签: php class

我的php示例返回一个有效的数组,但我想将所有内容保留在“类CreateAccount”中。

这是我今天做的方式,虽然看起来不太好,但可以。有更好的方法吗?

<?php
class CreateAccount {

     public function __construct($id,$namne,$fromdate,$group,$comment,$firstnamne,$org,$sid,$todate)
    {
        $this->countrows = null;
        $this->id = $id;
        $this->namne = $namne;
        $this->fromdate = $fromdate;
        $this->group = $group;
        $this->comment = $comment;
        $this->firstname = $firstnamne;
        $this->org = $org;
        $this->sid = $sid;
        $this->todate = $todate;
    }
}

/* Fill your Contact Object */
$contact = new CreateAccount("x6529","Jon Doe","2019-01-16","STD_GROUP","Guest User","Helen Doe","HQ","200410201234","2019-01-17");

/* Set your parameters for the request */
$params = array(
  "application" => 'api',
  "account" => $contact,
);

print_r($params);
?>


Array
(
    [application] => api
    [account] => CreateAccount Object
        (
            [countrows] => 
            [id] => x6529
            [namne] => Jon Doe
            [fromdate] => 2019-01-16
            [group] => STD_GROUP
            [comment] => Guest User
            [firstname] => Helen Doe
            [org] => HQ
            [sid] => 200410201234
            [todate] => 2019-01-17
        )

)

2 个答案:

答案 0 :(得分:0)

PHP具有功能get_object_vars,用于获取对象的所有属性。而且因为您要在构造函数中创建动态属性。因此,我们定义了一种方法getAccount,该方法将获取数组$properties中的所有属性,然后对其进行迭代,并将所有信息放入一个数组$account中,最后返回此数组。 / p>

<?php
class CreateAccount {

    public function __construct($id,$namne,$fromdate,$group,$comment,$firstnamne,$org,$sid,$todate)
    {
        $this->countrows = null;
        $this->id = $id;
        $this->namne = $namne;
        $this->fromdate = $fromdate;
        $this->group = $group;
        $this->comment = $comment;
        $this->firstname = $firstnamne;
        $this->org = $org;
        $this->sid = $sid;
        $this->todate = $todate;
    }

    public function getAccount()
    {
        $properties = get_object_vars($this);
        $account    = [];
        foreach($properties as $property)
        {
            $account[$property] = $this->{$property};
        }
        return $account;
    }
}

/* Fill your Contact Object */
$contact = new CreateAccount("x6529","Jon Doe","2019-01-16","STD_GROUP","Guest User","Helen Doe","HQ","200410201234","2019-01-17");

/* Set your parameters for the request */
$params = array(
  "application" => 'api',
  "account" => $contact->getAccount(),
);

print_r($params);
?>

答案 1 :(得分:-1)

我最终变成了这样的ling子:

class CreateAccount {

    public function __construct($application,$id,$name,$fromdate,$group,$comment,$firstname,$org,$sid,$todate)
    {
        $this->application = $application;
        $this->gastkonto['countrows'] = null;
        $this->gastkonto['id'] = $id;
        $this->gastkonto['name'] = $name;
        $this->gastkonto['fromdate'] = $fromdate;
        $this->gastkonto['group'] = $group;
        $this->gastkonto['comment'] = $comment;
        $this->gastkonto['firstname'] = $firstname;
        $this->gastkonto['org'] = $org;
        $this->gastkonto['sid'] = $sid;
        $this->gastkonto['todate'] = $todate;
    }
}

/* Fill your Contact Object */
$contact = new CreateAccount("api","x6529","Jon Doe","2019-01-16","STD_GROUP","Guest User","Helen Doe","HQ","200410201234","2019-01-17");

print_r($contact);