/    Sign up×
Community /Pin to ProfileBookmark

Advice structuring my framework?

Hi!

I am making my own framework. Why? I thought it would be a good learning exercise.

I am worried that I am programming myself into a dead end.

This is how it currently works.

index.php -> Set constants -> Load system class.

system class -> check url is valid -> Load relevant controller


—–

All Controllers extend base.
—–

system class runs the controllers _init() function. This loads models, language files and other relevant stuff.

So far so good. However, there is one major problem. The controller has all of the functionality. This isn’t a massive problem. I can load classes into my models and other libraries however this is a little impractical. To make this ultraclear.

[CODE]// Init Controller
class hi extends base {
public function _init() {
$this->hi_model = $this->load->model(‘hi’);
$this->db = $this->load->library(‘db’);
$this->val = $this->load->library(‘validation’);
}
}

// Model
class hi_model extends model {
public function index() {
$this->db->get_all(); // Error no function db
}
}[/CODE]

Do you see what I mean? Is there anyway to share the loaded classes amongst all classes?

Can anyone spot any obvious flaws in the current architecture?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 13.2009 — One possibility:
[code=php]
<?php
// Init Controller
class hi extends base {
public function _init() {
$this->db = $this->load->library('db');
$this->hi_model = $this->load->model('hi', $this->db);
$this->val = $this->load->library('validation');
}
}

class model
{
protected $db;
public function __construct(library $db=null)
{
if(!empty($db))
{
$this->db = $db;
}
}
}

// Model
class hi_model extends model {
public function index() {
$this->db->get_all();
}
}
[/code]

Another alternative would be to simply leave it to each class that needs to access the DB to do its own loading of the DB class. In this case it might be a good situation to use a Singleton pattern for the DB class, as long as you go into it willing to accept some of the potential drawbacks of that pattern.
Copy linkTweet thisAlerts:
@toicontienDec 13.2009 — Or you could have several abstract sub classes of "base" which could allow you to do some database handling automatically.
[code=php]abstract class DBConnectorBase extends base {

public function __construct() {
// set up database connection
}

public function __destruct() {
// tear down database connection
}

// Require all implementing classes to define a public function _init
abstract public function _init();

}


class MyController extends DBConnectorBase {
public function _init() {
// handle request
}
}[/code]
×

Success!

Help @Perad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...