dtcaw02086 2018-10-30 01:10
浏览 18
已采纳

初始化具有相同名称的php类

If we have two or more classes with the same name, but in different folders, how would one differentiate between them during initialization?

require_once('database.php');
require_once('t/database.php');

$db = new Database(); // I want this to initialize the one in t/database.php
$globalDb = new Database(); // I want this to initialize database.php
  • 写回答

2条回答 默认 最新

  • dr6673999 2018-10-30 03:51
    关注

    This is what Namespaces are for.

    But honestly, this sounds like an architectural problem. There are two ways you can approach this. If we were to answer your question as posed, you could solve your problem like so:

    database.php

    class Database{
        // ...
    }
    

    t/database.php

    namespace T;
    class Database{
        // ...
    }
    

    index.php

    require_once('database.php');
    require_once('t/database.php');
    
    $db = new T\Database();
    $globalDb = new Database();
    

    But judging by your naming conventions, it appears as if you have two separate classes that are intended to interact with either the same - or similar - database instances.

    I'm making some assumptions here, but the other way you can set up your logic is to condense your code down to a single Database class and operate on multiple connections over multiple instances.

    Consider using dependency injection to manage your connections in a single unified class structure, especially if you're using the same type of RDBMS flavor across all connections.

    Consider something like the following naive example:

    class Database{
        private $conn;
        public function __construct(PDO $conn){
            $this->conn = $conn;
        }
        public function select(...$args) { // Select Logic goes here }
        public function insert(...$args) { // Insert Logic goes here }
        public function update(...$args) { // Update Logic goes here }
        public function delete(...$args) { // Delete Logic goes here }
    }
    

    It would be possible to operate over multiple connections at once by simply injecting different PDO instances into the same class:

    require_once('database.php');
    $conn1 = new PDO('mysql:host=localhost;dbname=test1', $user, $pass);
    $conn2 = new PDO('mysql:host=localhost;dbname=test2', $user, $pass);
    $db1 = new Database($conn1);
    $db2 = new Database($conn2);
    

    So while the first example may address your question directly, you may want to rethink your architecture if I'm on the right track with my second example.

    And to echo everyone else, you should seriously consider using a proper Autoloader. Look into Composer and Autoloading - specifically PSR-4.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助