Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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 无法重新声明类-检查类是否已存在_Php_Oop_Class - Fatal编程技术网

Php 无法重新声明类-检查类是否已存在

Php 无法重新声明类-检查类是否已存在,php,oop,class,Php,Oop,Class,我正在使用一个脚本,该脚本两次调用同一个类并抛出错误: Fatal: Cannot redeclare class urlConverter (/var/www/core/cache/includes/elements/modsnippet/23.include.cache.php:14) 我已尝试在中放置以下代码: if( !class_exists( 'urlConverter', false ) ) { $urlConverter = new urlConverter( $mod

我正在使用一个脚本,该脚本两次调用同一个类并抛出错误:

Fatal: Cannot redeclare class urlConverter (/var/www/core/cache/includes/elements/modsnippet/23.include.cache.php:14)
我已尝试在中放置以下代码:

if( !class_exists( 'urlConverter', false ) )
{
    $urlConverter = new urlConverter( $modx );
}
然而,我正在使用的CMS报告了一个错误500,并且无法在日志中看到它抛出该错误的原因

有人知道如何检查该类是否已正确声明吗

编辑:

我使用的是CMS,因此该类被封装在一个代码段中,而不是一个实际的文件中。以下是他们如何称呼自己的代码片段:

$data['viewonlinelink'] = $this->modx->runSnippet( 'urlConverter', array(
                                            'action' => 'encrypt',
                                            'string' => http_build_query( $string ) ) );
我需要再打一次电话才能得到不同的结果

编辑2:

以下是URL转换器:

<?php
class urlConverter {

public $modx;

public function __construct( modX &$modx )
{
    $this->modx =& $modx;
}


public function action( $scriptProperties )
{
    return $this->$scriptProperties['action']( $scriptProperties['string'] );
}

private function encrypt( $str )
{
    return $str;
}


private function decrypt( $str )
{
      return $str;
}


}
}
 $urlConverter = new urlConverter( $modx );
 return $urlConverter->action( $scriptProperties );

它在我的urlConverter类中运行函数encrypt,我应该会收到两个不同的结果。

看起来定义该类的文件被多次包含,这就是为什么会出现第一个错误(无法重新声明类)

/var/www/core/cache/includes/elements/modsnippet/23.include.cache.php
line
14
似乎多次包含了这个类。如果可能,将
include
更改为
include\u一次
,这样就不会多次定义类

要进一步调试(而不是看到500内部服务器错误),请尝试尽早将以下内容添加到代码中:

error_reporting(E_ALL);
ini_set('display_errors', 1);

否则,请检查服务器
error\u log
(Apache)并查看其中是否有关于500错误的有用信息。

重新声明类
错误不是由创建类的新实例引起的,它是通过在同一符号上调用
操作符来调用的。您可能多次包含
urlConverter
类定义文件。

如果无法修改将类文件引入CMS的方式(使用
require\u once
include\u once
),请修改代码段:

if( !class_exists( 'urlConverter' ) ) {
    class urlConverter {
        /* Build class */
    }
}

当然,如果同一文件中还有其他内容,您需要确保它不会尝试运行两次。

在使用更新的代码时,将类文件更改为:

<?php
if(!class_exists('urlConverter')){
    class urlConverter {

        public $modx;

        public function __construct( modX &$modx ){
            $this->modx =& $modx;
        }
        public function action( $scriptProperties ){
            return $this->$scriptProperties['action']( $scriptProperties['string'] );
        }

        private function encrypt( $str ){
            return $str;
        }


        private function decrypt( $str ){
          return $str;
        }

    }
 }
$urlConverter = new urlConverter( $modx );
return $urlConverter->action( $scriptProperties );

您是否包括/需要类文件?如果是这样的话,用
include\u once
require\u once
来代替。这可能是一个require/include,你正在某处复制。我有点像include。我正在使用一个叫做MODx的CMS。我将使用他们CMS中的require语句修改我的问题。在查看在线文档时,您可能需要修改代码片段:尝试将
include
更改为
include\u once
@TimWithers我假设您指的是(file\u存在($file)){$o=include$file;}否则{$o='文件未在:'.$file;}退还$o;我想我可以试试。。我不是这样引用他们的片段,但这不会有什么坏处。是的,这似乎是对的。我不知道怎么才能不那样做。我正在使用的CMS会自动执行该操作,我没有太多选择。@Peter如果您不能修改CMS,请修改代码以仅创建以前未创建的类(而不是实例)。@JonathanSampson这是什么意思?嗯。。这抑制了错误,但当我第二次尝试从类中获取输出时,它没有返回任何内容。@Peter你能用你目前正在尝试的内容更新你的问题吗?这应该是唯一一个你称之为
class\u的地方了
。哦,我现在明白了!MODx使用PDO将类缓存到它们的注册表中。这很有效。
<?php
if(!class_exists('urlConverter')){
    class urlConverter {

        public $modx;

        public function __construct( modX &$modx ){
            $this->modx =& $modx;
        }
        public function action( $scriptProperties ){
            return $this->$scriptProperties['action']( $scriptProperties['string'] );
        }

        private function encrypt( $str ){
            return $str;
        }


        private function decrypt( $str ){
          return $str;
        }

    }
 }
$urlConverter = new urlConverter( $modx );
return $urlConverter->action( $scriptProperties );