php - How to use class property in another class ?
问题描述
Here is a scenario. we have following class.
<?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: which use packet object
<?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();
}
}
We have to use depost object for creating shipment following way
class ShippingClient implements IShippingClient
{
public function createShipment(depost $depost)
{
// here is code for creating shipment
}
}
Now the problem is when we creating packet object with following property we are not able to get in depost object. Kindly give me good way to fix this issue we cant able to change ShippingClient which use depost object.
解决方案
You can't able to get directly packet object data in depost object. You have to implement getter and setter method like following way.
class depost {
public $packet;
public function __construct(packet $packet)
{
$this->packet = $packet;
}
/**
* @return \packet
*/
public function getPacket() {
return $this->packet;
}
}
After that, you can access and use its property of packet object.
$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);
推荐阅读
- python - 在 Python 中使用 Nifi ExecuteScript 将附件上传到 Confluence
- reporting - COGNOS:维度 Cube 中时间和其他类别的聚合
- dataframe - PySpark Dataframe 显示方法无法正常工作
- php - 如何在 symfony 上检索包含在另一种形式中的输入的值?
- regex - 如何使用 grok 从日志中提取服务 url 组件
- firebase - 上传时ref.getDownloadURL为null,如何等待才能将其放入其他变量
- ios - Today 小部件中的 UIButton 未调用已连接的 IBAction 且未显示图像
- c# - 如何添加具有子属性的节点并为其设置值?
- python - 关于 Pytorch 中的奇数图像尺寸
- flutter - 堆栈中的手势被阻止