play_ Posted August 30, 2010 Share Posted August 30, 2010 Hello. Trying to learn MVC better by creating my own little framework to understand how it works. Things were going OK til now. I have a base class: <?php /** * Base class most classes will extend from. * Simply put, this class just has methods that * most, if not all, classes will need. */ class Application { public function includer($path) { if (is_readable($path) == true) { include_once($path); } else { die("404 not found =["); } } } ?> So the above method 'includer' just sees if a file exists/is readable and if so, include it. Here's where I am using said method. class Index extends Application { function __construct($method = 'view') { // load the index model include(ROOT . '/app/models/model.index.php'); // Invoke requested method. $this->$method(); } public function view() { $name = 'Smith'; $this->includer(ROOT . '/app/views/view.index.php'); # Problem here, i think } } view.index.php just contains <?php echo $name; ?> All that is called from the index page, with this line of code Application::includer($controller_path); Now, in line $this->includer(ROOT . '/app/views/view.index.php'); # Problem here, i think If i get rid of $this->includer, the script will work and say 'Smith'. If i have $this->includer(...) or parent::includer(...), it doesn't work. why? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 30, 2010 Share Posted August 30, 2010 $name is local to the view function and you are including the view file in a separate function. There are multiple ways to do this, but what comes to mind are the following: You either need to pass the vars to the includer function (maybe as an array) and then extract() them there. Or better, at some point set a class var $this->data or something and use that when you need it. I'll give this some more thought after lunch Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 30, 2010 Share Posted August 30, 2010 I would probably move the view() method into the Application class or build a View class and have it extract the view vars that would either be set in the controller using $this->view_vars or a method of the View class set_vars() or something similar. Quote Link to comment Share on other sites More sharing options...
play_ Posted August 30, 2010 Author Share Posted August 30, 2010 thanks. yea not sure which one of those 2 to do yet. trying to keep this as simple as possible Quote Link to comment 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.