Organize and relate class in PHP -
well, trying adapt old code more object oriented model find difficulties in doing it. structure of classes 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, see, there incorrect things in code. want have inside main
class, create $main
object , there access other object. db
need constant config
yet don't want create new config
inside db
use 1 main
. same happens info.
the problem should fixed 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);
Comments
Post a Comment