dongxuandong2045 2014-08-07 20:48
浏览 81
已采纳

PHP - 这是好的require_once还是扩展

I'm a newbie in php but I'll try to get straight to the point. I have a class called ConnectionManager

class ConnectionManager
{
     function ConnectToDB()
     {
         //PDO connection code  
     }
}

and in my other manager InstitutManager I am using require_once($filename) to get access to my ConnectionManager functions

require_once('../manager/ConnectionManager.php');
class InstitutManager
{
     protected $connInstance;

     function _construct()
     {
         $this->connInstance = new ConnectionManager;
     }

     function getInstituts()
     {
         $conn = $connManager->ConnectToDb();
         //retrieve instituts 
     }
}

The question is : Should I be using extends ConnectionManager in my InstitutManager instead of require_once? Why should I use one more than the other?

Thanks

Edit : Changed code for InstitutManager class

Would this be ok like this? Or should I pass a pass a parameter with my connection already instanciated in function _construct($conn)?

  • 写回答

2条回答 默认 最新

  • duan19850312 2014-08-07 20:56
    关注

    Your include_once reads in a source file, which in this case has a class definition for ConnectionManager in it. Your extends sets up class InstitutManager as inheriting class ConnectionManager, i.e. InstitutManager gets everything in ConnectionManager and can then define its own modifications to that basic structure. There isn't really any relationship at all between the two operations, and your $connManager = new ConnectionManager operations are nonsensical.

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

报告相同问题?