Zend How to create object of object


#1

Hi,

I am newbie on Zend framework. There is an object inside object as the following tradtional PHP script. If I want to implement on Zend controller or model, how I can write it on Zend? I have tried stdClass but not seems to work exactly.

class LevyEFrankRequest
{
    public $API_KEY;
    public $RECEIPT_NUMBER;
    public $OUT_BOUND_FARE_PAID;
    public $NAME_OF_TRAVELER_PAYER;
    public $DEPARTURE_DATE;
    public $LAST_4_DIGIT_OF_CONTACT_PHONE_NUMBER;
}

$request = new LevyEFrankRequest();
$request->API_KEY = '87062F452DD245E89661F5A2E9F8720A';
$request->RECEIPT_NUMBER = '20180003';
$request->OUT_BOUND_FARE_PAID = '105.65';
$request->NAME_OF_TRAVELER_PAYER = "Anonymous";
$request->DEPARTURE_DATE = "2018-12-31";
$request->LAST_4_DIGIT_OF_CONTACT_PHONE_NUMBER = '3775';

$data = new LevyEFrank();
$data->request = $request;

var_dump result :

object ( LevyEFrank )[ 2 ] 
    public 'request' =>;
       object (LevyEFrankRequest)[1] 
           public 'API_KEY' => <small>string</small> '87062F452DD245E89661F5A2E9F8720A' (length=32) 
           public 'RECEIPT_NUMBER' =>; <small>string</small> '20180003' (length=8)
           public 'OUT_BOUND_FARE_PAID' =><small>string</small> '105.65' (length=6)
           public 'NAME_OF_TRAVELER_PAYER' =><small>string</small> 'Anonymous' (length=9)
           public 'DEPARTURE_DATE' => <small>string</small> '2018-12-31' (length=10)
           public 'LAST_4_DIGIT_OF_CONTACT_PHONE_NUMBER' =><small>string</small> '3775' (length=4)

#2

Heya,

What you are doing there is plain PHP: no framework involved (and that’s ok!).

You can put that code anywhere you like within your setup, as it is just procedural instantiation of your Levy* objects.


#3

I have tried to create the object on zend controller. It shows the following error.

Fatal error: Class declarations may not be nested


#4

Heya,

Where you declare the classes is up to you. This is generally handled via autoloading: see https://phptherightway.com/#namespaces