Jump to content

Recommended Posts

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?

 

 

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

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();
}

}

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.

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.

$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.

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.