Jump to content

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.