具有数据结构的PHP类定义

时间:2012-06-12 22:28:01

标签: php oop object

我要做的是以下内容......在我的其他课程中加入 CLASS FILE ,然后执行以下操作:

    $zSubscription = new zSubscription();
    $zSubscription->Account->AccountNumber = "ABCD-230948";
    $zSubscription->BillToContact->Address1= "123 Dirt Lane Road";
    $this->createContact($zSubscription);


public function createContact($subscriptionObj){
    if (strlen($subscriptionObj->Account->AccountNumber) > 10){
        // Do something special
        echo "Batch Processing<br />";
        if ($subscriptionObj->Account->Batch == "Batch1"){
            echo "Running Batch 1";
        }
    }
}

应设置BATCH1(这是预期的结果)目前尚未设置。

要通过require_once包含的类文件如下:     

/**
* Zuora Definition Class
* @author Will H
* @license MIT
*/

class Account{
    /** @var string */
    public $AccountNumber  = null;
    /** @var bool */
    public $AllowInvoiceEdit   = true;
    /** @var bool */
    public $AutoPay = true;
    /** @var string */
    public $Batch    = "Batch1";
    /** @var string */
    public $BcdSettingOption    = "ManualSet";
    /** @var int */
    public $BillCycleDay     = 31;
    /** @var string */
    public $CrmId;
    /** @var string */
    public $Currency = "USD";
    /** @var string */
    public $CustomerServiceRepName = "Online Order";
    /** @var string */
    public $Name;
    /** @var string */
    public $PaymentTerm = "Due Upon Receipt";
    /** @var string */
    public $PurchaseOrderNumber = null;
    /** @var string */
    public $SalesRepName = "Online Order";
}

class PaymentMethod{ 
    /** @var string */
    public $CreditCardAddress1;
    /** @var string */
    public $CreditCardAddress2;
    /** @var string */
    public $CreditCardCity;
    /** @var string */
    public $CreditCardCountry;
    /** @var int */
    public $CreditCardExpirationMonth;
    /** @var int */
    public $CreditCardExpirationYear;
    /** @var string */
    public $CreditCardHolderName;
    /** @var string */
    public $CreditCardNumber;
    /** @var string */
    public $CreditCardPostalCode;
    /** @var string */
    public $CreditCardState;
    /** @var string */
    public $CreditCardType;
    /** @var string */
    public $Type = 'CreditCard';
}
class BillToContact{

    /** @var string */
    public $Address1;
    /** @var string */
    public $Address2;
    /** @var string */
    public $City;
    /** @var string */
    public $Country;
    /** @var string */
    public $FirstName;
    /** @var string */
    public $LastName;
    /** @var string */
    public $PostalCode;
    /** @var string */
    public $State;
    /** @var string */
    public $WorkEmail;
    /** @var string */
    public $WorkPhone;

}
class SubscribeOptions{
    /** @var bool */
    public $GenerateInvoice = true;
    /** @var bool */
    public $ProcessPayments = true;
}

class Subscription{
    /** @var bool */
    public $AutoRenew = true;
    /** @var string */
    public $ContractAcceptanceDate;
    /** @var string */
    public $ContractEffectiveDate;
    /** @var int */
    public $InitialTerm = 12;
    /** @var int */
    public $RenewalTerm = 12;
    /** @var string */
    public $ServiceActivationDate;
    /** @var string */
    public $TermStartDate;
}
class RatePlan{
    /** @var string */
    public $ProductRatePlanId;
}
class RatePlanData{
    /** @var RatePlan */
    public $RatePlan;
}
class SubscriptionData{
    /** @var Subscription */
    public $Subscription;
    /** @var RatePlanData */
    public $RatePlanData;
}
class zSubscription{
    /** @var Account */
    public $Account;
    /** @var PaymentMethod */
    public $PaymentMethod;
    /** @var BillToContact */
    public $BillToContact;
    /** @var SubscribeOptions */
    public $SubscribeOptions;
    /** @var SubscriptionData */
    public $SubscriptionData;
}

?>

1 个答案:

答案 0 :(得分:1)

要说,你的代码泄漏封装,将是轻描淡写。根本没有封装。您可以用数组替换“对象”,并将其称为一天。

在写得很好的课程中,您只有privateprotected个变量。它们仅在 Getters Setters 时可用,即便如此,您也必须小心谨慎。您应该阅读这篇文章:"GetterEradicator" by Martin Fowler

代码中的另一个问题是你似乎在构造函数中做了一些伏都教。看一下这个例子:

$zSubscription = new zSubscription();
$zSubscription->Account->AccountNumber = "ABCD-230948";
$zSubscription->BillToContact->Address1= "123 Dirt Lane Road";

AccountBillToContact来自哪里?您正在公开对象(在zSubscription::__construct()方法中创建的对象...因此导致与两个类的名称紧密耦合)到完全不相关的上下文。这是Law of Demeter违规,导致抽象漏洞。