Properly using classes in other classes in php?

前端 未结 3 1567
终归单人心
终归单人心 2020-12-28 11:06

Should have asked someone this a long time ago.

What is the best way to use other classes within another class?

For instance, lets say I have an application

3条回答
  •  一整个雨季
    2020-12-28 11:29

    $db could be a property of your Application class. Any reference to it from within an instance of Application would be done via $this - $this->db

    class Application {
    
      private $db = null;
    
      public function setDB($name) {
        $this->db = new Database($name);
      }
    
    }
    

提交回复
热议问题