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
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 »

He said "class", not "object". I don't think the missing $ is a typo; he wanted to know if he could tell a method to create an instance of some class. Plus I assume he already knows he can pass an object by reference, and he may even know that objects are passed reference-by-value by default.
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:He said "class", not "object". I don't think the missing $ is a typo; he wanted to know if he could tell a method to create an instance of some class. Plus I assume he already knows he can pass an object by reference, and he may even know that objects are passed reference-by-value by default.
You're right, I asked if I can pass a class reference.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post by josh »

There is no such thing a class reference. A class is just "DNA" ("the blueprint"), the object is the "living organism" (the actual "object"). References are variables. References can point to objects. Classes you cannot 'point' to, objects you can.
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.
By the way, is it possible to pass a reference of a class in function?
Like:

Code: Select all

$Object = new Object(&Object2);
That is not a lot of work for the functionality it provides. Being able to serve articles over APIs, over HTTP... in HTML, pdf, text, json, xml, etc..

For example if you have an HTML view, a print view, and an XML (api)... thats 3 different views, 1 model, 1 controller. This is with MVC (3 tier).

With no layering, you'd still have 3 files, but each file would contain more (duplicated) code.

Contrary to what other members have stated, model & controller in the same class is not called a transaction script, its called 2 tier. A transaction script would correspond with 1 tier, all code in one file, not even separating out the HTML.***

It is fine to choose 1 tier, 2 tier or 3 tier, and is a subjective choice based on the requirements of your application, and future growth expectancy.

*** Actually POEAA is not clear on this IMO, it states that a transaction script can include "displaying data from a database", to me that implies presentation+Business logic, implying 1 tier. In another sentence it states a transaction script should not have a dependency on presentation logic, implying 2 tier. I think it may include both 1 tier OR 2 tier?
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 »

Jonah Bron wrote:He said "class", not "object".
Technical wrote:You're right, I asked if I can pass a class reference.
I see that now, I just assumed you really meant an object. Woops.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post by josh »

My comment was directed at the OP, you used the correct term (reference to an object). The OP was the only to mention the impossible concept of "reference to a class"
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 »

Okay, okay, I was wrong. I didn't know much about references, never used them.
So here is my next question: would you recommend to use namespaces? Aren't they too new?

This article says you can use Scope Resolution Operator, but it produces syntax error.
Last edited by Technical on Mon Dec 20, 2010 3:54 am, edited 1 time in total.
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 »

Finally, I've come with acceptable and good(I think) solution.
So I made Item and Set classes.
Then, I made

Code: Select all

ContentItem extends Item
and

Code: Select all

ContentSet extends Set
.
Content class is a View.

For example,

Code: Select all

$Item = new ContentItem(1); // Item with ID 1
Content::Item($Item);

$Set = new ContentSet(0, 10); // 10 items
Content::Items($Set);
Thanks everyone for help, I see now that MVC is really better and more useful
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post by greyhoundcode »

Technical wrote:So here is my next question: would you recommend to use namespaces? Aren't they too new?
Well, in terms of being too new, I suppose the only problem is if you don't have control over the hosting environment, ie you're making something that end-users will deploy on shared hosting and some of those same accounts aren't running 5.3+.

Otherwise, I like them. Incidentally, I posted a question recently regarding autoloading with namespaces and Zyxist provided a good link that may be of interest, if you want to look into it.

groups.google.com/group/php-standards/web/psr-0-final-proposal

But let's not get in trouble for going off topic ;-)
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post by Technical »

greyhoundcode wrote:
Technical wrote:I suppose the only problem is if you don't have control over the hosting environment, ie you're making something that end-users will deploy on shared hosting and some of those same accounts aren't running 5.3+.
Sadly, this problem is fatal for me. I checked the biggest hosting provider's site in our country. They said they have PHP version 5, which is not so much information.
Okay, what about Scope Resolution Operator?
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 »

So far as namespaces go, here is the sort of syntax that works:

Code: Select all

// twitter.php
namespace social;

class Twitter {
	public function __construct()	{
		echo 'I interact with Twitter';
	}
}

Code: Select all

// index.php

include 'twitter.php';
$tweet = new social\Twitter;
I have also seen some stuff on the web suggesting you can use the old Paamayim Nekudotayim :: in relation to working with namespaces, but that isn't documented here. Perhaps that was an idea that was tested and discarded prior to the official 5.3 release. Don't know if anyone else can shed some light?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Shared hosting - PHP versions - 5.2 popular just now

Post by greyhoundcode »

Technical wrote:Sadly, this problem is fatal for me. I checked the biggest hosting provider's site in our country. They said they have PHP version 5, which is not so much information.
Not that this is the definitive source or anything, but these stats from WordPress suggest PHP 5.2 is grabbing much of the limelight.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post by Weirdan »

greyhoundcode wrote: I have also seen some stuff on the web suggesting you can use the old Paamayim Nekudotayim :: in relation to working with namespaces, but that isn't documented here. Perhaps that was an idea that was tested and discarded prior to the official 5.3 release. Don't know if anyone else can shed some light?
Yes, it was originally suggested way to access anything namespaced, but that was replaced by current namespace separator (\) because of the ambiguity it would cause in cases like this:

Code: Select all

// file1.php
namespace something;
function doSomething() {};

Code: Select all

// file2.php
class something {
   public static function doSomething() {}
}

Code: Select all

// main.php
include 'file1.php';
include 'file2.php';
something::doSomething(); // are you accessing static method of the class or standalone namespaced function here?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post by josh »

Technical wrote:Finally, I've come with acceptable and good(I think) solution.
So I made Item and Set classes.
Then, I made

Code: Select all

ContentItem extends Item
and

Code: Select all

ContentSet extends Set
.
Content class is a View.

For example,

Code: Select all

$Item = new ContentItem(1); // Item with ID 1
Content::Item($Item);

$Set = new ContentSet(0, 10); // 10 items
Content::Items($Set);
Thanks everyone for help, I see now that MVC is really better and more useful
FYI the example code you shown is not really having anything to do with MVC.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post by Weirdan »

josh wrote:There is no such thing a class reference. [...] References are variables. References can point to objects. Classes you cannot 'point' to, objects you can.
... in PHP.
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 »

josh wrote:FYI the example code you shown is not really having anything to do with MVC.
Excuse me, what is wrong with my code?
I have two Models - ContentItem and ContentSet, I have one View - Content, while Controller is being the module itself. There is no SQL or HTML in the Controller, no HTML in the Models.
Post Reply