1

NOTE ON QUESTION TITLE: I did not really even know how to phrase the title so if someone can edit and make it more articulate I would really appreciate it.

QUESTION:

Basically, what I'm trying to do is create a main "App" class that has private access to the database and config objects and provide an interface for accessing config properties as well as using database methods.

I am new to the whole interface thing and have not used it previously. I may be going about this all wrong and appreciate any help anyone can give in pointing me in the right direction. Interface may not be the way to go, I don't know, but it seemed like the right way to do this.

Also, I'm trying to integrate this with some old code which uses a dependency container as a sort of interface I guess. I will include that below but not as crucial for me on that because I am trying to phase that out with this new interface. But wanted to include it for completeness in case it matters in some way.

Currently getting errors about not being able to access private property of app->_cfg but I'm sure I can work through that, I'm more interested in knowing if I am on the right track here. If what I'm doing is good practice or am I way off.

So going to try to summarize this in code as clearly as possible without pasting a wall of code here...

//overloaded if that matters
class config {
//config stuff, db logins, etc
}

$cfg = new config();

class mysqli {
//db stuff, connect to db, select, insert etc..
}

//Dependency Container mentioned above, not cruicial
//hoping to remove this
class depcon {

    public static $_database;
    public static $_config;

    public static function createDatabaseObject() {

        $db = new mysqli();
        $db->setConfig(self::$_config);
        $db->connect();

        return $db;
    }
}

//hand off config object to container
depcon::$_config = $cfg;

//use container to create object, injecting config
$db = depCon::createDatabaseObject();

//set container db object to obj it just created
depCon::$_database = $db;

class app {

    private $_db;
    private $_cfg;

    public function __contstruct($db, $cfg) {
        $this->setDatabase($db);
        $this->setConfig($cfg);
    }

    private function setConfig($cfg) {
        $this->_cfg = $cfg;
    }

    private function setDatabase($db) {
        $this->_db = $db;
    }
}

$app = new app($db, $cfg);

interface iuser {
    public function getUsers();
    public function addUser(array $data);
}

class user implements iuser {

    private $_app;
    private $users;

    public function __construct($app) {
        $this->setApp($app);
    }

    private function setApp($app) {

        $this->_app = $app;
    }

    public function getUsers() {
        return $this->_app->_db->select('allUsers');
    }

    public function addUser(array $data) {
        return $this->_app->_db->insert($data);
    }
}

Thank you for any help. Please let me know if there are any questions.

  • I can't quite tell what your actual question is, unless it's just "Is this good?" which would be considered too broad/subjective. Is there something specific you're concerned about? – Ixrec May 2 '15 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.