Page 1 of 3

MVC - Controller and Model in the same class?

Posted: Sun Dec 12, 2010 5:28 am
by Technical
Hello, I'm rewriting my old code to MVC structure and I have a question:
Is it fine to one class working as Model and Controller at the same time?
For example:

Code: Select all

class SomeClass
{
    function __construct() //Retrieves data from database
    static function Display($SomeClass) // Using instance of this class and calls template functions
}

Re: MVC - Controller and Model in the same class?

Posted: Sun Dec 12, 2010 4:11 pm
by Jonah Bron
To my knowledge, no. It's arguably okay to have your view and controller in the same class, but not the controller and model. They are two separate objects.

Re: MVC - Controller and Model in the same class?

Posted: Sun Dec 12, 2010 8:56 pm
by Christopher
Technical wrote:Is it fine to one class working as Model and Controller at the same time?
It is fine to combine your Model and Controller (if you know what you are doing). It is called the Transaction Script pattern. You code would no longer be MVC though. There are cases where it can make sense. However, in general keeping your Model independent of the Presentation code is considered a core best practice.
Technical wrote:Hello, I'm rewriting my old code to MVC structure and I have a question:
Maybe you aren't! ;)

Re: MVC - Controller and Model in the same class?

Posted: Fri Dec 17, 2010 8:22 am
by Technical
Thanks for replies.
Before MVC structure, I had two classes for each module - for single item and for item collection. But I think single item class is enough, I can just have array of single item objects. Is it memory efficient?

Re: MVC - Controller and Model in the same class?

Posted: Fri Dec 17, 2010 11:30 am
by Jonah Bron
That would depend on whether or not you need methods that operate on the set.

Re: MVC - Controller and Model in the same class?

Posted: Fri Dec 17, 2010 11:42 am
by Technical
No, I don't have anything to do with collections except displaying.

Re: MVC - Controller and Model in the same class?

Posted: Fri Dec 17, 2010 12:50 pm
by Jonah Bron
Then arrays will work just fine. 8)

Re: MVC - Controller and Model in the same class?

Posted: Fri Dec 17, 2010 1:01 pm
by Technical
Fine, but would you recommend such decision?

Re: MVC - Controller and Model in the same class?

Posted: Sat Dec 18, 2010 12:43 pm
by Jonah Bron
Yes, as long as the array is handled by some object (not necessarily a dedicated object), and not thrown about in a procedural sort of way.

Re: MVC - Controller and Model in the same class?

Posted: Sat Dec 18, 2010 1:31 pm
by Technical
MVC structure is starting to disappoint me. Hell, I need to create 3 classes for just to display a single article. Is it really worth it? Before I just had Display() method in each class.
By the way, is it possible to pass a reference of a class in function?
Like:

Code: Select all

$Object = new Object(&Object2);

Re: MVC - Controller and Model in the same class?

Posted: Sat Dec 18, 2010 2:34 pm
by greyhoundcode
Technical wrote:MVC structure is starting to disappoint me. Hell, I need to create 3 classes for just to display a single article. Is it really worth it?
Don't be too dismayed, it needn't be so complex. Consider this post by Rasmus Lerdorf.

Re: MVC - Controller and Model in the same class?

Posted: Sat Dec 18, 2010 2:53 pm
by Jonah Bron
Technical wrote:MVC structure is starting to disappoint me. Hell, I need to create 3 classes for just to display a single article. Is it really worth it? Before I just had Display() method in each class.
If your application is too simple, MVC may not be the right pattern in this case.
Technical wrote:By the way, is it possible to pass a reference of a class in function?
Like:

Code: Select all

$Object = new Object(&Object2);
No, you can't. But you can pass a string like this:

Code: Select all

$Object = new Object('Object2');
And handle it like this:

Code: Select all

__construct($class) {
    if (class_exists($class)) {
        $object = new $class();
    }
}
Pretty slick, huh?

Re: MVC - Controller and Model in the same class?

Posted: Sun Dec 19, 2010 3:46 am
by Technical
Jonah Bron wrote:
Technical wrote:MVC structure is starting to disappoint me. Hell, I need to create 3 classes for just to display a single article. Is it really worth it? Before I just had Display() method in each class.
If your application is too simple, MVC may not be the right pattern in this case.
I wouldn't call it so simple, but MVC makes it more complex.

So what I have, for example content module:

1) ContentItem class - used to create and edit articles. Has __construct method which queries database by id and fills object properties; Display() method for obvious needs; Save() method for saving into database.
2) ContentCollection - used to display a group of items with certain criteria like search. Has __construct method which queries database and fills the array and Display method.[/list]

What I plan to do:

1) Create universal model Collection for uses like

Code: Select all

$Collection = new Collection('ContentItem');
2)Controller will be represented not by class[/list]
So ContentItem and Collection are models, module file is a controller and Content class is a view.

Code: Select all

$Item = new ContentItem(1); // For single item
Content::Item($Item);
$Collection = new Collection('ContentItem', 0, 10);
Content::Items($Collection);
I think that will be fine.

Re: MVC - Controller and Model in the same class?

Posted: Sun Dec 19, 2010 1:53 pm
by greyhoundcode
Technical wrote:By the way, is it possible to pass a reference of a class in function?
Like:

Code: Select all

$Object = new Object(&Object2);
Jonah Bron wrote:No, you can't.
Perhaps I am misunderstanding something, but why not? Other than that the syntax isn't quite right, objects are by default passed by reference.

Code: Select all

class Zebra 
{
	public $electronicTag;
	
	
	public function __construct() 
	{
		$this->electronicTag = 1;
	}
}


class Scientist
{
	public $myZebra;
	
	
	public function __construct($zebraO)
	{
		$this->myZebra = $zebraO;
		$this->myZebra->electronicTag = 2;
	}
}


// Here is my new zebra 
$martie  = new Zebra;

// And here is his electronic tag number
echo $martie->electronicTag;

// I'll pass a reference to Martie to my scientist,
// who wishes to track Martie.
// For his own purposes, this boffin will change
// the electronic tag number of his allocated
// zebra, ie Martie.
$boffin = new Scientist($martie);

// And because we passed a reference to Martie,
// Martie's own tag has changed.
echo $martie->electronicTag;

Re: MVC - Controller and Model in the same class?

Posted: Sun Dec 19, 2010 1:56 pm
by greyhoundcode
Just to add, of course were it a real world example we could use type-hinting to enforce class type etc, but KISS and all that.