ShoeLace1291 Posted June 19, 2012 Share Posted June 19, 2012 So I'm in the process of coding a VERY light MVC framework... only have run into one problem thus far. I can't seem to get my models to load. Undefined property: Test::$member in W:\wamp\www\basecmd\controllers\test.php on line 8 This is controllers/test.php <?php class Test extends Controller { function index(){ $this->load->model('member'); $data = $this->member->info(); $this->load->view('test_view'); } } This is the controller class: <?php class Controller { public $load; public $model; function __construct(){ $this->load = new Load; } } This is the load class: <?php class Load { function view($filename = '', $data){ if(is_array($data)){ extract($data); } if(file_exists(APP_PATH.'/views/'.$filename.'.php')){ include(APP_PATH.'/views/'.$filename.'.php'); } else { die('Requested view could not be loaded: '.$filename); } } function model($filename){ if(file_exists(APP_PATH.'/models/'.$filename.'.php')){ include(APP_PATH.'/models/'.$filename.'.php'); $this->$filename = new $filename; } else { die('Requested model could not be loaded: '.$filename); } } function controller($classname){ if(file_exists(APP_PATH.'/controllers/'.$classname.'.php')){ include(APP_PATH.'/controllers/'.$classname.'.php'); $$classname = new $classname; } else { die('Requested controller could not be loaded: '.$classname); } } function system_class($classname){ if(file_exists(APP_PATH.'/system/'.$classname.'.php')){ include(APP_PATH.'/system/'.$classname.'.php'); $this->$classname = new $classname; } else { die('Requested system class could not be loaded: '.$classname); } } } And finally, my front controller(index.php) <?php define('APP_PATH', dirname(__FILE__)); define('APP_SUBDIRECTORY', '/basecmd'); require_once(APP_PATH.'/system/load.php'); $load = new Load; require_once(APP_PATH.'/system/controller.php'); require_once(APP_PATH.'/controllers/test.php'); $test = new Test; $test->index(); ?> Note: My front controller is just a test for now. I still have to come up with a way to route the URL's to the correct class/method. Any help is appreciated! Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/ Share on other sites More sharing options...
trq Posted June 19, 2012 Share Posted June 19, 2012 Nowhere in your code do you define a $member property within your controller. On a side note, why aren't you using autoloading? Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355107 Share on other sites More sharing options...
ShoeLace1291 Posted June 19, 2012 Author Share Posted June 19, 2012 Oh, sorry, forgot to include this one... members.php model <?php class Member { function info(){ return array( 'first' => 'Hello, ', 'last' => 'World!' ); } } And it is called in the Test controller. Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355114 Share on other sites More sharing options...
trq Posted June 19, 2012 Share Posted June 19, 2012 The problem still remains. This line: $data = $this->member->info(); No where have you set member. Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355124 Share on other sites More sharing options...
ShoeLace1291 Posted June 19, 2012 Author Share Posted June 19, 2012 I thought that's what $this->load->model('member") does... right? Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355132 Share on other sites More sharing options...
trq Posted June 19, 2012 Share Posted June 19, 2012 No. That sets your model as a property of your Load object. Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355133 Share on other sites More sharing options...
trq Posted June 19, 2012 Share Posted June 19, 2012 Also, again, why are you trying to write such a convoluted system? What is wrong with using an autoloader? Quote Link to comment https://forums.phpfreaks.com/topic/264432-help-with-my-custom-mvc/#findComment-1355135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.