Symfony2,在另一个自定义类

时间:2016-10-10 09:28:18

标签: php symfony

如何在不使用服务容器的Symfony 2中的自定义类中加载/访问自定义类?

如果我尝试使用如下所示的自定义记录器,我收到错误:

Catchable Fatal Error: Argument 1 passed to MeetingBundle\Components\LogWriter::__construct() must implement interface Symfony\Component\HttpKernel\Log\LoggerInterface, none given, called in C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\just2\src\MeetingBundle\Components\Serializer\Diff\EventDiff.php on line 23 and defined

堆栈追踪:

in src\MeetingBundle\Components\LogWriter.php at line 12  -
private $logger;
public function __construct( LoggerInterface $logger ) // here they show the error
{
    $this->logger = $logger;
} 

SRC \ MeetingBundle \组件\ LogWriter.php

<?php

namespace MeetingBundle\Components;

use Symfony\Component\HttpKernel\Log\LoggerInterface;

class LogWriter
{

    private $logger;

    public function __construct( LoggerInterface $logger )
    {
        $this->logger = $logger;
    }

    public function log($msg)
    {
        $this->logger->log($msg);
    }
}

SRC \ MeetingBundle \组件\串行\ DIFF \ Diff.php

<?php

namespace MeetingBundle\Components\Serializer\Diff;

//use MeetingBundle\Components\LogWriter;

class Diff 
{
    private $diffCluesArr; 
    private $name; 
    private $logWriter;

    public function __construct ($logger) {
        $this->name= ""; 
        $this->diffCluesArr = [];
        $this->logWriter = $logger; 
        //$this->logWriter = new $logWriter; //changed here 
    } 

    public function array_diff_str_o ( $arr1, $arr2, $str ) {
        $this->logWriter("<br> In Diff  function array_diff_str_o ");
   //...

SRC \ MeetingBundle \组件\串行\ DIFF \ EventDiff.php

<?php

namespace MeetingBundle\Components\Serializer\Diff;

use MeetingBundle\Components\Serializer\Diff\Diff;
use MeetingBundle\Components\LogWriter;

/**
 * Event normalizer
 */
class EventDiff extends Diff
{
    private $diffCluesArr; 
    private $name; 
    private $logw;

    // does not work
   // public function __construct (LogWriter $logger) {
   //     $this->logw= $logger;
  //      parent::__construct($this->logw);

    public function __construct () {
        $this->logw = new LogWriter();
        parent::__construct($this->logw);    
        $this->logw("<br> In constructor of EventDiff");
        $this->name= "event"; 
        $this->diffCluesArr = array( 
                //1 means compare normally
                //2 means compare the values of the keys
                'id' => 1,
// ..

应用\设置\ services.yml

services:
    meeting.logw:
        class:     MeetingBundle\Components\LogWriter
        arguments: ["@logger"]

    meeting.diff.diff:
        class:     'MeetingBundle\Components\Serializer\Diff\Diff' 
        arguments: ["@meeting.logw"]

    meeting.diff.event:
        class: 'MeetingBundle\Components\Serializer\Diff\EventDiff'
        parent: meeting.logw
        #the same error: parent: meeting.diff.diff

SRC \ MeetingBundle \控制器\ EventMapController.php

//.. 
    $diffentiator = $this->get('meeting.diff.event');
    $diffentiator->array_diff_str_o( $arrEventOld, $arrEventNew, $msg );
//..

// ****问题的旧版本

如果我尝试使用如下所示的自定义记录器,我收到错误:

  

捕获致命错误:传递给MeetingBundle \ Components \ Serializer \ Diff \ Diff :: __ construct()的参数1必须是MeetingBundle \ Components \ Serializer \ Diff \ LoggerInterface的实例,没有给定,在C:\ Bitnami \中调用第16行的wampstack-5.6.20-0 \ apache2 \ htdocs \ sym \ just2 \ src \ MeetingBundle \ Components \ Serializer \ Diff \ EventDiff.php并定义

错误在哪里?代码如下:

just2\src\MeetingBundle\Components\LogWriter.php

<?php

namespace MeetingBundle\Components;

use Symfony\Component\HttpKernel\Log\LoggerInterface;

class LogWriter
{

    private $logger;

    public function __construct(LoggerInterface $logger) // the place of error
    {
        $this->logger = $logger;
    }

    public function log($msg)
    {
        $this->logger->log($msg);
    }
}

just2\src\MeetingBundle\Components\Serializer\Diff\Diff.php

<?php

namespace MeetingBundle\Components\Serializer\Diff;

use MeetingBundle\Components\LogWriter;

class Diff 
{
    private $logWriter;

    public function __construct (LoggerInterface $logger) {
      //  the first mistake
        $this->logWriter = $logger;
    }

    public function array_diff_str_o ( $arr1, $arr2, $str ) {
        $this->logWriter("<br> In Diff  function array_diff_str_o "); 
    //.. 
}

// src \ MeetingBundle \ Components \ Serializer \ Diff \ EventDiff.php

<?php

namespace MeetingBundle\Components\Serializer\Diff;

use MeetingBundle\Components\Serializer\Diff\Diff;
/** Provides clue how to calculate the differences between entities instances */
class EventDiff extends Diff
{
   private $diffCluesArr; 
   private $name; 

    public function __construct () {
        parent::__construct();
        $this->logWriter("<br> In constructor of EventDiff");
        $this->name= "event"; 
        $this->diffCluesArr = array( 
                //1 means compare normally
                //2 means compare the values of the keys
                'id' => 1,
//...

just2 \ src \ MeetingBundle \ Controller \ EventMapController.php

    /** * @Route("/edit/{id}", name="event_jsMap_edit")
     * @Method("GET|POST")
     * @Template("MeetingBundle:Event:ev_jsMap_edit.html.twig")
     */
    public function editAction($id, Request $request)
    {
...
$diffentiator = $this->get('meeting.diff.event');
$diffentiator->array_diff_str_o( $arrEventOld, $arrEventNew, $msg ); 
...

我还将logwrite作为服务,但也许没有必要,我不希望它成为一项服务。我想将它作为一个单独的类而不是作为服务容器的一部分使用: 应用\配置\ services.yml

services:
    events.logger:
        class:     MeetingBundle\Components\LogWriter
        arguments: ["@logger"]

    meeting.diff.event:
        class: 'MeetingBundle\Components\Serializer\Diff\EventDiff'

    #class Diff is not a service. Class EventDiff extends from Diff.

2 个答案:

答案 0 :(得分:0)

问题很可能是您在Diff类中实例化EventDiff的方式,如错误消息所示。 LogWriter似乎没问题。

您是否将EventDiff定义为具有正确依赖关系的服务?

修改:在EventDiff中,您在没有任何参数的情况下调用parent::__construct();。但是,父Diff类接受一个参数。您可能希望向EventDiff注入一个服务,该服务将被传递给构造函数中的父项。

答案 1 :(得分:0)

你可以Manage Common Dependencies with Parent Services。尝试按以下方式定义定义parent属性的服务:

services:
    events.logger:
        class:     MeetingBundle\Components\LogWriter
        arguments: ["@logger"]

    meeting.diff.event:
        class: 'MeetingBundle\Components\Serializer\Diff\EventDiff'
        parent: events.logger

希望这个帮助