PHP - 无法动态实例化类 [英] PHP - Can't dynamcally instantiate class

查看:32
本文介绍了PHP - 无法动态实例化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suddenly, I cant dynamically instantiate a class. I can instantiate it if I call it directly, but calling it with a variable won't work. Here's whats not working:

    $database_class = 'MysqlConnection';

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

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

The first instantiation, making $class, works fine. The second, making $other_class, fails and gives the following error:

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

PHP Fatal error: Class 'MysqlConnection' not found in /pronounce-php/src/Database/Connect.php on line 47

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

What am I doing wrong here? Heres the whole file if it helps:

<?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.这是因为文字在文字范围内是固定的,并且名称解析是明确的.

The actual name of the class is PronouncePHP\Database\Connection\MysqlConnection. Since you've aliased it at the top of the file you can refer to it as MysqlConnection using literals. That's because literals are fixed in literal scope and name resolution is unambiguous.

然而,变量中的字符串可以来自任何时间的任何地方,因此实际上无法根据 use 语句解析.如果要将名称用作字符串变量,则需要使用完全限定名称:

However, strings in variables can come from anywhere any time and can hence not realistically be resolved against use statements. If you want to use the name as a string variable, you need to use the fully qualified name:

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

这篇关于PHP - 无法动态实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆