dongliao2011 2015-06-24 06:25
浏览 54
已采纳

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();

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

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;
    }
}
  • 写回答

1条回答 默认 最新

  • dongqiabei7682 2015-06-24 07:30
    关注

    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.

    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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'