Jump to content

Using mutiple classes question


x1nick

Recommended Posts

Don't quite know where I got this idea from but wondering if im going along the wrong lines of thinking.

 

Basically I have a main class called `cms` where all my main functions are held for including files etc

 

Now within my other class, for example `mysql` I want to be able to access the data set within cms->cfg

Now what is the best way to do this without using global $cms, thorugh

class mysql extend cms

May work but that dosent

 

class cms {
var $cfg = array();

function addcfg($key,$value) {
$cfg[$key] = $value;
}
}
$cms = new cms();

 

mysql:

 

class mysql extend cms {
function connect() {
$user = $this->cfg['username'];
#do my sql connect..
}
}
$db = new mysql();

 

So basically how can I get access to the cms class within mysql without using global.

Link to comment
https://forums.phpfreaks.com/topic/202991-using-mutiple-classes-question/
Share on other sites

In case you don't understand what Mchl wants to say or you don't understand the article, he meant:

 

class CMS {
  private $mysql = null;
  
  public function initMySQL() {
    $this->mysql = ..
  }
}

 

favor composition over inheritance

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.