I think I just had some clickage going on about MVC.

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

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

I think I just had some clickage going on about MVC.

Post by s.dot »

I realize that MVC (a design pattern) is a separate concept from object orientation. But as I'm learning, I think I just had something click in my head about utilizing object orientation with MVC.

I was on my paypal account and noticed the url. cmd=something. Sweet, a command! It's telling the script to do something. So I don't know why this just now clicked in my head, but that command is controlling what the script executes and displays.

Usually, procedurally, I will just use an if/else/elseif on the $_GET['cmd'] or whatever other variable I'd be using. But I kept thinking about the word control. This command (or any parameter really.. this just happened to be the first one that clicked for me) could be used to design a controller (I think...).

So, I wrote this. It's very elementary and straight and to the point (but hey, im learning), and it could use better use of __get() and __set().. but you will get the idea.

Code: Select all

<?php

class command
{
	private $_command;
	private $_validCommands;
	private $_defaultCommand;
	
	public function setValidCommands($commands)
	{
		$this->_validCommands = $commands;
	}
	
	public function setDefaultCommand($command)
	{
		$this->_defaultCommand = $command;
	}
	
	public function setCommand($command)
	{
		$this->_command = $command;
	}
	
	public function getCommand()
	{
		return in_array($this->_command, $this->_validCommands) ? $this->_command : $this->_defaultCommand;
	}
}
So, I could use this object to control what goes on by using it like the following.

Code: Select all

//instantiate the object
$cmd = new command();

//set allowed commands
$allowedCommands = array('create', 'edit', 'view');
$cmd->setValidCommands($allowedCommands);

//set current command
$cmd->setCommand($_GET['cmd']);

//do something with this....?
$model = new model($cmd->getCommand());
Do I have the right idea going on here? Did something click right... or wrong? Or maybe somewhere in between?

Any comments you could add on this would be appreciated, really. When something clicks with me I get all excited and research/code writing happy.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

What you're doing is basically a Configuration, some people tend to rather use Conventions:

Code: Select all

class command 
{
	function run() {
		$cmd = $_GET['cmd'];
		if(!method_exists($this, "cmd_$cmd"))
			$cmd = 'index';
		return $this->{"cmd_$cmd"}();
	}
	function cmd_create() { do create stuff }
	function cmd_edit() { do edit stuff }
}
This class doesn't provide an explicit list of commands, but rather relies on a convention that every method starting with "cmd_" is a valid command.

The advantage of Conventions is less verbosity and ease to extend at the price of some obscurity, because unlike Configurations they are not always obvious.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Code: Select all

//instantiate the object 
$cmd = new command(); 

//set allowed commands 
$allowedCommands = array('create', 'edit', 'view'); 
$cmd->setValidCommands($allowedCommands); 

//set current command 
$cmd->setCommand($_GET['cmd']); 

//do something with this....? 
$model = new model($cmd->getCommand());
The script above resembles something of a front controller, so your shooting for a model 2 MVC design.

Here is the gist of the idea:

1) Front controller takes command and decides which 'action' controller is invoked.
2) Action controller then uses required model(s) and generates appropriate view.

You probably want to refactor the 'model' code into an action controller.

When you develop scripts and have separate pages such as:

- create_user.php
- update_user.php
- remove_user.php

Apache is emulating a front controller. HTTP passes along a script name which is then invoked by a web server (Apache). That script then needs to work with a model and generate a view.

If you wish to implement a front controller directly in PHP, you basically need to implement a similar method of script invocation - although not nessecarily a script (could be a function could be a class, etc). That script is then responsible for dealing with the views and models for that request.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: I think I just had some clickage going on about MVC.

Post by Christopher »

scottayy wrote:I realize that MVC (a design pattern) is a separate concept from object orientation. But as I'm learning, I think I just had something click in my head about utilizing object orientation with MVC.

...

Do I have the right idea going on here? Did something click right... or wrong? Or maybe somewhere in between?

Any comments you could add on this would be appreciated, really. When something clicks with me I get all excited and research/code writing happy.
A couple of things. First there is conceptual connection with the Front Controller pattern which you are approaching and the MVC pattern which you are clicking. That they are often used together in implementation is a separate discussion than discussions about the individual patterns.

What you have written is the front end, validation code for a Front Controller, Request Mapper or Router. I don't think you should pass it to the Model for a couple of reasons. First, the Model is hopefully in a different layer than the request handling code. So it makes more sense for the request handling code to tell the Model which mode it should be in rather than passing some request data directly to it. Second, Models are usually specific to a specific data domain, so they would be used for a specific purpose rather than just generally as you show it.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I realize that MVC (a design pattern) is a separate concept from object orientation.
Not really. Design patterns are solutions for organising objects, they are recipes and objects the ingredients; loosely.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ole wrote:
I realize that MVC (a design pattern) is a separate concept from object orientation.
Not really. Design patterns are solutions for organising objects, they are recipes and objects the ingredients; loosely.
I agree with ole here. There is more of a connection between OOP and patterns that you think. That is because OOP is the mindset from which patterns emerged, so the solutions tent to be OO in nature.
(#10850)
Post Reply