简体   繁体   English

Symfony2,在另一个自定义类中使用/使用自定义类

[英]Symfony2, acess/use the custom class in another custom class

How to load/access a custom class in the custom class in Symfony 2 not using service container? 如何在Symfony 2中的自定义类中加载/访问自定义类而不使用服务容器?

If i try to use the custom logger as shown below i am getting the error: 如果我尝试使用如下所示的自定义记录器,我收到错误:

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

Stack Trace: 堆栈跟踪:

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\\Components\\LogWriter.php 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\\Components\\Serializer\\Diff\\Diff.php 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\\Components\\Serializer\\Diff\\EventDiff.php 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,
// ..

app\\config\\services.yml 应用程序\\ CONFIG \\ 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\\Controller\\EventMapController.php SRC \\ MeetingBundle \\控制器\\ EventMapController.php

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

//**** THE OLD VERSION OF THE QUESTION // ****问题的旧版本

If i try to use the custom logger as shown below i am getting the error: 如果我尝试使用如下所示的自定义记录器,我收到错误:

Catchable Fatal Error: Argument 1 passed to MeetingBundle\\Components\\Serializer\\Diff\\Diff::__construct() must be an instance of MeetingBundle\\Components\\Serializer\\Diff\\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 16 and defined 可捕获致命错误:传递给MeetingBundle \\ Components \\ Serializer \\ Diff \\ Diff :: Diff :: __ construct()的参数1必须是MeetingBundle \\ Components \\ Serializer \\ Diff \\ LoggerInterface的实例,没有给出,在C:\\ Bitnami \\ wampstack-5.6中调用第16行的.20-0 \\ apache2 \\ htdocs \\ sym \\ just2 \\ src \\ MeetingBundle \\ Components \\ Serializer \\ Diff \\ EventDiff.php并定义

Where is the mistake? 哪里出错了? The code is as follow: 代码如下:

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 // 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 just2的\\ src \\ MeetingBundle \\控制器\\ 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 ); 
...

I also made logwrite to be a service, but maybe it is not necessary and i do not want it to be a service. 我还将logwrite作为服务,但也许没有必要,我不希望它成为一项服务。 I would like to use it as a individual class not as a part of a service container: app\\config\\services.yml 我想将它作为一个单独的类使用,而不是作为服务容器的一部分:app \\ config \\ 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.

The problem is most likely in the way you instantiate Diff in EventDiff class as the error message suggests. 问题很可能是您在EventDiff类中实例化Diff的方式,如错误消息所示。 LogWriter seems allright. LogWriter似乎没问题。

Do you define EventDiff as a service as well with correct dependencies? 您是否将EventDiff定义为具有正确依赖关系的服务?

Edit: In EventDiff you're calling parent::__construct(); 编辑:在EventDiff你正在调用parent::__construct(); without any parameter. 没有任何参数。 However the parent Diff class takes one parameter. 但是,父Diff类需要一个参数。 You probably want to inject a service to EventDiff that'll be passed to its parent in the constructor. 您可能希望向EventDiff注入一个服务,该服务将在构造函数中传递给它的父级。

You could Manage Common Dependencies with Parent Services . 您可以使用父服务管理公共依赖项 Try defining the service defining the parent attribute in the following manner: 尝试按以下方式定义定义parent属性的服务:

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

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

Hope this help 希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM