dongliao2011
dongliao2011
2015-06-24 06:25

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 dongqiabei7682 6年前

    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';
    
    点赞 评论 复制链接分享

相关推荐