PHP-无法动态实例化类 - php

突然,我无法动态实例化一个类。如果直接调用它,我可以实例化它,但是用变量调用它是行不通的。这是行不通的:

    $database_class = 'MysqlConnection';

    $class = new MysqlConnection();
    $other_class = new $database_class();

第一个实例化为$class,可以正常工作。第二个出现$other_class,失败并给出以下错误:

PHP致命错误:在第47行的/pronounce-php/src/Database/Connect.php中找不到类'MysqlConnection'

我在这里做错了什么?如果有帮助,则继承整个文件:

<?php

namespace PronouncePHP\Database;

use Symfony\Component\Console\Output\OutputInterface;
use PronouncePHP\Database\Connection\MysqlConnection;

class Connect
{
    private $config;

    /**
     * Construct
     *
     * @return void
    */
    public function __construct()
    {
        $this->config = include('config.php');
    }

    /**
     * Get database connection
     *
     * @return Connection
    */
    public function getDatabaseConnection($output)
    {
        $database_type = $this->getDatabaseType($output);

        $database_class = $this->makeConnectionClass($database_type);

        $connection_information = $this->getConnectionInformation($database_type);

        // if (!class_exists($database_class))
        // {
        //     $output->writeln("<error>Database type not found!</error>");
        //     $output->writeln("<comment>Please ensure that the database type is specified and that it is supported</comment>");

        //     $GLOBALS['status'] = 1;

        //     exit();
        // }
        $database_class = "MysqlConnection";

        $class = new MysqlConnection();
        $other_class = new $database_class();
    }

    /**
     * Get database type specified in config file
     *
     * @return string
    */
    public function getDatabaseType($output)
    {
        $database_type = $this->config['database'];

        if (is_null($database_type))
        {
            $output->writeln("<error>No database type specified in config.php</error>");

            $GLOBALS['status'] = 1;

            return null;
        }

        return $database_type;
    }

    /**
     * Make class name for connection
     *
     * @return string $database_type
    */
    public function makeConnectionClass($database_type)
    {
        return ucfirst($database_type) . 'Connection';
    }

    /**
     * Get connection information for specified database type
     *
     * @return string $database_type
    */
    public function getConnectionInformation($database_type)
    {
        $information = $this->config['connections'][strtolower($database_type)];

        return $information;
    }
}

参考方案

该类的实际名称是PronouncePHP\Database\Connection\MysqlConnection。由于您已经在文件的顶部添加了别名,因此可以使用文字将其称为MysqlConnection。这是因为文字在文字范围内是固定的,并且名称解析是明确的。

但是,变量中的字符串可以随时随地出现,因此实际上不能针对use语句进行解析。如果要将名称用作字符串变量,则需要使用标准名称:

$database_class = 'PronouncePHP\Database\Connection\MysqlConnection';

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

PHP Laravel从另一个驱动器读取文件 - php

我目前正在学习Laravel。我想知道是否有一种方法可以在Laravel中使用Storage::从另一个硬盘访问文件(使用Windows)。例如,我在驱动器C:上安装了带有Laravel的Xampp,但是我想访问网站目录之外的E:上的文件。我试过使用Storage::files('E:')和File::files('E:…