MVC - Controller and Model in the same class?
Moderator: General Moderators
- 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?
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.
Re: MVC - Controller and Model in the same class?
You're right, I asked if I can pass a class reference.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.
Re: MVC - Controller and Model in the same class?
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.
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?
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..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);
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?
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: MVC - Controller and Model in the same class?
Jonah Bron wrote:He said "class", not "object".
I see that now, I just assumed you really meant an object. Woops.Technical wrote:You're right, I asked if I can pass a class reference.
Re: MVC - Controller and Model in the same class?
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"
Re: MVC - Controller and Model in the same class?
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.
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.
Re: MVC - Controller and Model in the same class?
Finally, I've come with acceptable and good(I think) solution.
So I made Item and Set classes.
Then, I made and .
Content class is a View.
For example,
Thanks everyone for help, I see now that MVC is really better and more useful
So I made Item and Set classes.
Then, I made
Code: Select all
ContentItem extends ItemCode: Select all
ContentSet extends SetContent 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);
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: MVC - Controller and Model in the same class? (Namespace
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+.Technical wrote:So here is my next question: would you recommend to use namespaces? Aren't they too new?
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
Re: MVC - Controller and Model in the same class? (Namespace
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.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+.
Okay, what about Scope Resolution Operator?
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: MVC - Controller and Model in the same class?
So far as namespaces go, here is the sort of syntax that works:
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?
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;- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Shared hosting - PHP versions - 5.2 popular just now
Not that this is the definitive source or anything, but these stats from WordPress suggest PHP 5.2 is grabbing much of the limelight.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.
Re: MVC - Controller and Model in the same class?
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: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?
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?
Re: MVC - Controller and Model in the same class?
FYI the example code you shown is not really having anything to do with MVC.Technical wrote:Finally, I've come with acceptable and good(I think) solution.
So I made Item and Set classes.
Then, I madeandCode: Select all
ContentItem extends Item.Code: Select all
ContentSet extends Set
Content class is a View.
For example,
Thanks everyone for help, I see now that MVC is really better and more usefulCode: Select all
$Item = new ContentItem(1); // Item with ID 1 Content::Item($Item); $Set = new ContentSet(0, 10); // 10 items Content::Items($Set);
Re: MVC - Controller and Model in the same class?
... in PHP.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.
Re: MVC - Controller and Model in the same class?
Excuse me, what is wrong with my code?josh wrote:FYI the example code you shown is not really having anything to do with MVC.
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.