Well, I am trying to adapt my old code to a more object oriented model but I find difficulties in doing it. The structure of my classes would look like this:
// config.php
class Config {
const Setting1 = 'value';
const Setting2 = 'value';
}
// main.php
include 'config.php'
class Main {
var $Config;
var $Info;
var $Db;
function _construct() {
$this->Config = &new Config;
$this->Info = &new Info;
$this->Db = &new Db($this);
}
}
class Info {
function getSetting($a, $Config) {
if ($a>0) return $Config::Setting1;
return $Config::Setting2;
}
}
class Db {
function _construct($Main) {
$Setting1 = $Main->Config::Setting1;
}
}
// index.php
$Main = new Main;
echo $Main->Info->getSetting(1, $Main->Config);
So, as you see, there are incorrect things in this code. What I want is to have everything inside the Main
class, create the $Main
object and from there access any other object.
Db
will need a constant from Config
yet I don't want to create a new Config
inside Db
but use the one from Main
. The same happens with Info.
The problem should be fixed with this:
// config.php
class Config {
public static $Setting1 = 'value';
public static $Setting2 = 'value';
}
// main.php
include('config.php');
class Main {
private $Info = null;
private $Db = null;
public function _construct() {
$this->Info = new Info();
$this->Db = new Db();
}
public function getSetting($a) {
return $this->Info->getSetting($a);
}
}
class Info {
public function getSetting($a) {
if ($a>0) return Config::$Setting1;
return Config::$Setting2;
}
}
class Db {
public function _construct() {
$Setting1 = Config::$Setting1;
}
}
// index.php
$Main = new Main();
echo $Main->getSetting(1);