首页 > 解决方案 > 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.

标签: phpoop

解决方案


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

推荐阅读