开发者

Alternative to accessing classes from within classes

I'm having real problems getting my head around classes, and am hoping someone can a开发者_运维百科nswer my (very basic!) question

I have a mysql class allowing me to setup a mysql connection, perform queries and other related mysql functions.

I've got a database setup with a parent\child structure, and have a recursive function (my createNode function) that outputs a set of nested lists reflecting the parent\child structure.

The createNode function instantiates the mysql class on every recursion in order to query the required data from the database.

Ideally I would like to put this createNode function into a class, but I understand it is bad practise to instantiate one class into another class, however I can't see any way around this.

How can I structure my classes to get around this problem?

Thanks in advance!


class Noder {
  private $_conn;
  public function __construct($mysql_connection)
  {
    $this->_conn = $mysql_connection;
  }
  public function createNode()
  {
    // whatever you do
  }
}
$mysql = new Mysql();
$noder = new Noder($mysql);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜