Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2,访问/使用另一个自定义类中的自定义类_Php_Symfony - Fatal编程技术网

Php Symfony2,访问/使用另一个自定义类中的自定义类

Php Symfony2,访问/使用另一个自定义类中的自定义类,php,symfony,Php,Symfony,如何在不使用服务容器的情况下在Symfony 2中的自定义类中加载/访问自定义类 如果我尝试使用如下所示的自定义记录器,则会出现错误: Catchable致命错误:传递给MeetingBundle\Components\LogWriter:的参数1::\uu construct()必须在第23行的C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\just2\src\MeetingBundle\Components\Serializer\Diff\E

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

如果我尝试使用如下所示的自定义记录器,则会出现错误:

Catchable致命错误:传递给MeetingBundle\Components\LogWriter:的参数1::\uu construct()必须在第23行的C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\just2\src\MeetingBundle\Components\Serializer\Diff\EventDiff.php中实现接口,未给定,并已定义

堆栈跟踪:

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

<?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\Controller\EventMapController.php

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

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

如果我尝试使用如下所示的自定义记录器,则会出现错误:

可捕获的致命错误:传递给MeetingBundle\Components\Serializer\Diff\Diff::\uu构造()的参数1必须是MeetingBundle\Components\Serializer\Diff\LoggerInterface的实例,未给出任何实例,在第16行的C:\Bitnami\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);
    }
}
<?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;
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,
// ..
<?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,
//...
我也让logwrite成为一种服务,但也许它不是必需的,我不希望它成为一种服务。我希望将其用作单个类,而不是服务容器的一部分: app\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
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.

如错误消息所示,问题很可能是在
EventDiff
类中实例化
Diff
的方式<代码>日志编写器似乎没问题

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


编辑:在
EventDiff
中调用
parent::\u construct()不带任何参数。但是,父级
Diff
类接受一个参数。您可能希望向
EventDiff
注入一个服务,该服务将在构造函数中传递给其父级。

问题很可能是您在
EventDiff
类中实例化
Diff
的方式,如错误消息所示<代码>日志编写器
似乎没问题

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

编辑:在
EventDiff
中调用
parent::\u construct()不带任何参数。但是,父级
Diff
类接受一个参数。您可能希望将服务注入到
EventDiff
,该服务将在构造函数中传递给其父级。

您可以。尝试以以下方式定义定义父属性的服务:

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

    meeting.diff.event:
        class: 'MeetingBundle\Components\Serializer\Diff\EventDiff'
        parent: events.logger
希望这对你有所帮助。尝试以以下方式定义定义父属性的服务:

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

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

希望获得此帮助

如何实例化
Diff
对象?能否演示如何将
EventDiff
定义为服务?Class`EventDiff`是从Class
Diff
扩展而来的服务。类
Diff
有几种方法,这些方法对于计算两个实例之间差异的所有实体都是通用的,如果是相同类型的实体,因此我从这个类扩展了每个
EntityDiff.php
。另外。类
Diff
有一个记录器来记录来自其方法的消息。错误似乎出现在记录器和logWriter变量中。有时我很累,会混淆变量。如何实例化
Diff
对象?能否演示如何将
EventDiff
定义为一个服务?Class`EventDiff`是一个服务,它从Class
Diff
扩展而来。类
Diff
有几种方法,这些方法对于计算两个实例之间差异的所有实体都是通用的,如果是相同类型的实体,因此我从这个类扩展了每个
EntityDiff.php
。另外。类
Diff
有一个记录器来记录来自其方法的消息。错误似乎出现在记录器和logWriter变量中。有时我很累,把各种变量混在一起。我对这个问题做了更正。它被定义为
meeting.diff.event:class:'MeetingBundle\Components\Serializer\diff\EventDiff'
。错误似乎出现在logger和logWriter变量中。有时我很累,会混淆变量。我更新了答案,你没有正确调用父构造函数。谢谢。如果没有服务注入,这可能吗?@olga我不这么认为:)。我对这个问题做了更正。它被定义为
meeting.diff.event:class:'MeetingBundle\Components\Serializer\diff\EventDiff'
。错误似乎出现在logger和logWriter变量中。有时我很累,会混淆变量。我更新了答案,你没有正确调用父构造函数。谢谢。没有服务注入这可能吗?@olga我不这么认为:)。
services:
    events.logger:
        class:     MeetingBundle\Components\LogWriter
        arguments: ["@logger"]

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