MVC - Controller and Model in the same class?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

MVC - Controller and Model in the same class?

Post 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
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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! ;)
(#10850)
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post by Jonah Bron »

That would depend on whether or not you need methods that operate on the set.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post by Technical »

No, I don't have anything to do with collections except displaying.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post by Jonah Bron »

Then arrays will work just fine. 8)
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post by Technical »

Fine, but would you recommend such decision?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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);
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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?
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post 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.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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;
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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.
Post Reply