【发布时间】:2015-04-18 17:35:16
【问题描述】:
我发现了一个问题,我不确定是 php 的错误还是我的代码(可能是我的)的错误,所以让我告诉你发生了什么:
<?php namespace MyApp\Conciliation;
use SimpleExcel\SimpleExcel;
use ForceUTF8\Encoding;
use MyApp\Conciliation\Gol;
class Conciliation {
protected function equalizeFile($file, $providerName)
{
$type = false;
$nfile = 'public'.$file;
// TEST 1: the ideal aproach. not working (see error#1 bellow)
$provider = new $providerName();
// TEST 2: working, getting the correct response
$provider = new Gol();
// TEST 3: working, getting the correct response
$provider = new MyApp\Conciliation\Gol();
$provider->equalize($nfile);
}
注意,$providerName = 'Gol';
错误1 找不到类“Gol”
那么,有什么方法可以让我继续使用变量来实例化类似于上面的别名?
编辑,问题已解决:工作示例
<?php namespace MyApp\Conciliation;
use SimpleExcel\SimpleExcel;
use ForceUTF8\Encoding;
class Conciliation {
protected function equalizeFile($file, $providerName)
{
$type = false;
$nfile = 'public'.$file;
$providerName = "MyApp\\Conciliation\\".$providerName;
$provider = new $providerName();
$provider->equalize($nfile);
}
【问题讨论】:
标签: php namespaces composer-php