1

Im working with automatic code generator for my projects. And im using Zend Framework 2 for File/Class/Method reflection. I need to add elements to returned array:

 public function getServiceConfig()
 {
     return array(
         'factories' => array(
             'Album\Model\AlbumTable' =>  function($sm) {
                 $tableGateway = $sm->get('AlbumTableGateway');
                 $table = new AlbumTable($tableGateway);
                 return $table;
             },
             'AlbumTableGateway' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new Album());
                 return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
             },
         ),
     );
 }

I can get 'factories' array, but cannot add new element (functions). (I need to add new element to factories like " 'objectTableGateway' => function($sm){ ... }") After that i write method body like body = var_export(array, true);

  1. if i add just function - function executes;
  2. if i add in quotes - i need manually clean quotes after code writes to file.

How to solve this problem?

Only solution i see is generate whole method body as plain text

Tilo
  • 51
  • 3

1 Answers1

0
$arr = array(
  'factories' => array(
      'Album\Model\AlbumTable' =>  function($sm) {
          echo $sm;
      },
      'AlbumTableGateway' => function ($sm) {
          echo $sm;
      },
  ));

 $arr['factories']['Album\Model\AlbumTable']("Test");   

This seem to work for me. How do you execute the function?

 $arr['factories']['NewFunction'] = function($sm) {
     echo $sm . $sm;
 };

 $arr['factories']['NewFunction']("test");

adding a new one on the fly also works. You can also loop trough them

 foreach($arr["factories"] as $function) {
     $function("TEST2");
 }
Jeroen
  • 579
  • 5
  • 19
  • I need to write this array element to file. When i execute var_export($factories, 1); it returns: 'Album\\Model\\AlbumTable' => Closure::__set_state(array( )) – Tilo Aug 01 '14 at 10:21
  • 1
    Then I guess you problem is related to this http://stackoverflow.com/questions/13734224/exception-serialization-of-closure-is-not-allowed this is about serialization, but I think that solution could help you along as well. DaveRandom comment on the answer also explain why you get this. – Jeroen Aug 01 '14 at 10:49