如何在另一个类中使用class属性?

时间:2018-04-28 04:59:32

标签: php oop

这是一个场景。我们有以下课程。

<?php

/**
 * Class packet
 *
 * this class represents a consignment in packeta's system. It has been stripped of unnecessary properties.
 * This class should not be directly passed to carrier specific classes (API Client etc), to use this data in those,
 * please use a wrapper class (\depost) instead.
 *
 * @property int $id
 * @property int $barcode Packeta's own barcode/tracking nr
 * @property int $weight
 * @property int $adult_content
 *
 * @property float $cod
 * @property float $value Real value for insurance purposes
 * @property float $conversion_rate
 * @property float $lat
 * @property float $lon
 *
 * @property string $number receiver's own reference number
 * @property string $name
 * @property string $surname
 * @property string $company
 * @property string $email
 * @property string $phone
 * @property string $country
 * @property string $street
 * @property string $house
 * @property string $city
 * @property string $zip
 *
 * Datetime properties - intentionlly defined as string
 * @property string $created
 * @property string $delivered
 * @property string $consigned_date
 * @property-read string $consigned_datetime
 *
 * New added parameter
 *
 * @property string $iso2_name
 *

 */
class packet {

}

Depost Class:使用数据包对象

<?php

/**
 * Class depost
 *
 * this class is a wrapper for \packet class for external carrier purposes.
 *
 * @property string $number
 */

class depost {

    /**
     * @return \packet
     */
    public function getPacket() {
        return new packet();
    }
}

我们必须使用depost对象按照方式创建货件

class ShippingClient implements IShippingClient
{
     public function createShipment(depost $depost)
    {
      // here is code for creating shipment
    }
}

现在问题是当我们使用以下属性创建数据包对象时,我们无法获取depost对象。请给我一个很好的方法来解决这个问题我们无法改变使用depost对象的ShippingClient。

1 个答案:

答案 0 :(得分:0)

您无法直接在depost对象中获取数据包对象数据。你必须像下面那样实现getter和setter方法。

class depost {

    public  $packet;

    public function __construct(packet $packet)
    {
        $this->packet = $packet;
    }
    /**
     * @return \packet
     */
    public function getPacket() {
        return $this->packet;
    }
}

之后,您可以访问和使用其包对象的属性。

$oPacket = new packet();

$oPacket->id = '1';

$oPacket->barcode       = null;
$oPacket->weight        = null;
$oPacket->adult_content = null;

$oPacket->cod             = null;
$oPacket->value           = null;
$oPacket->conversion_rate = null;
$oPacket->lat             = null;
$oPacket->lon             = null;

$oPacket->number    = null;
$oPacket->name      = null;
$oPacket->surname   = null;
$oPacket->company   = null;
$oPacket->email     = null;
$oPacket->phone     = null;
$oPacket->country   = null;
$oPacket->street    = null;
$oPacket->house     = null;
$oPacket->city      = null;
$oPacket->zip       = null;
$oPacket->iso2_name = null;


$oPacket->currency = null;

$oPacket->created            = null;
$oPacket->delivered          = null;
$oPacket->consigned_date     = null;
$oPacket->consigned_datetime = null;

$oDepost = new depost($oPacket);






$oShippingClient = new ShippingClient();
    $response        = $oShippingClient->createShipment($oDepost);