darksniperx Posted July 29, 2011 Share Posted July 29, 2011 I have done mvc few years back, but forgot most of it. I am trying out mvc which i have found on the following link: http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/ when i need to access functions of model from controller class. How would i do that? I have home controller and home model. model has a function doThis(){...}; How do i execute that function from controller class? Quote Link to comment Share on other sites More sharing options...
trq Posted July 29, 2011 Share Posted July 29, 2011 I didn't look at the tutorial but you would simply make sure that the Model class is available (included) and then instantiate it as per usual. You might even make a helper method within your base controller that handles this for you. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 29, 2011 Author Share Posted July 29, 2011 here is what i did: <?php include('home.php'); class HomeController extends Controller { $model = new Home(); function index($id = null,$name = null) { $this->set('title',$name.' - First Test page'); $model->printText('123'); } } I get the following: Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /usr/local/www/vhosts/htdocs/application/controllers/homecontroller.php on line 4 Quote Link to comment Share on other sites More sharing options...
trq Posted July 30, 2011 Share Posted July 30, 2011 You cannot simply define variables where ever you like within a class. I suggest you create a private property to store your model in and then assigned it within your __construct. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 30, 2011 Author Share Posted July 30, 2011 Here is what i did: <?php class HomeController extends Controller { function __construct() { $model = new Home(); } function index($id = null,$name = null) { $this->set('title',$name.' - First Test page'); //$this->set('todo',$this->Item->select($id)); $model->printText('123'); } } Here is what it tells me only when i add __construct: Fatal error: Call to a member function set() on a non-object in /usr/local/www/vhosts/htdocs/library/controller.class.php on line 21 controller.class.php <?php class Controller { protected $_model; protected $_controller; protected $_action; protected $_template; function __construct($model, $controller, $action) { $this->_controller = $controller; $this->_action = $action; $this->_model = $model; $this->$model =& new $model; $this->_template =& new Template($controller,$action); } function set($name,$value) { #this line# $this->_template->set($name,$value); } function __destruct() { $this->_template->render(); } } Quote Link to comment Share on other sites More sharing options...
trq Posted July 30, 2011 Share Posted July 30, 2011 You might have read up on some basic OOP before simply copying and pasting code from the tutorial you've linked to. See oop5. In fact, even some simple tutorials on functions and variable scope would come in handy for you. Variables defined within a function are not available outside of that function. The base controller you just posted requires that the model, controller and action are passed into the __construct as params. This is a floored design IMO. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 30, 2011 Author Share Posted July 30, 2011 Edit: Ok, I have figured out what i needed to have: $this->Home->printText('123'); Home is the name of the model, while printText the name of the function. Quote Link to comment Share on other sites More sharing options...
trq Posted July 30, 2011 Share Posted July 30, 2011 But do you have any understanding of why or how this works? Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 30, 2011 Author Share Posted July 30, 2011 As far as i understand, $this refers to current class. Most likely there is an instance of Home class too somewhere. Since there is an instance of home class, calling its function works no problem. This is as far as i can explain it for now. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 30, 2011 Share Posted July 30, 2011 $this refers to the current object. $Home is an instance variable in that object, which is also in this case an object. printText() is a method defined in the class of the object $Home contains. I'm not sure why you arrived at this code or where you are using it -- but there are a often a number of ways to accomplish the same things in Oop. Judging from some of your prior code snippets, I guess that Home is an object of class Home() whatever that is. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 30, 2011 Author Share Posted July 30, 2011 Yes, Home is object of class home. The MVC that i am using, there isnt much documentation explaining every little detail. I searched though codes from various tutorials from the same author of MVC until l i came upon that line. And when i modified to work with my model and function it work. And it also did not required for me to do any includes. The main issues as i have stated above, i haven't touched php for a couple of years. Thats why it is a little slow for me at this time. Quote Link to comment Share on other sites More sharing options...
trq Posted July 30, 2011 Share Posted July 30, 2011 The main issues as i have stated above, i haven't touched php for a couple of years. Thats why it is a little slow for me at this time. This is indeed why I suggested reading up on some of the basics first. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted July 30, 2011 Author Share Posted July 30, 2011 thank you for all of the info! 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.