Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help and feedback with an MVC framework

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
Hi

To further educate myself I am putting together a new MVC framework in PHP from scratch. It is my first real big project using classes this way and i am a bit confused at some of the results.

This is a simplified version of what I have, any feedback in general would be greatly appreciated and answers to my specific questions even more so.

Either way thank you for having a look :)




The structure is generally:
Controller calls 3 main classes "System", "Model" and "View".

The system object holds all config information, system variables, database object, location data, names for the page module, templates, etc. based on the current url.

The Model object contains all the content data, static language texts, etc.
-> the model includes classes for module and widget data

The View object contains the xml and xsl code and content for outputting to the browser.
-> the view includes classes for master, module and widget templates.

In the below simplified example I have left out the View and greatly reduced the code of the other classes. I have also stripped out all the phpdocumentor and regular comments.




index.php
Code:
<?php
	require_once('Controller.php');
	$controller = new Controller();
?>
Note: in the actual framework additional functionality is added for passing the current url into the class and for displaying the page.

Controller.php
Code:
<?php
	
	require_once('System.php');
	require_once('Model.php');

	class Controller{
		
		protected $system = NULL;
		protected $model = NULL;	

		 public function __construct(){
			$this->system 	= new System();
			$this->model 	= new Model($this->system->get());
		}

	}


System.php
Code:
<?
	class System{

		private $id = 0;

		public function __construct(){
			$this->id=3;
		}

		public function get() {
			$return->id = $this->id;
			return $return;
		}

	}
Note: In the actual framework a lot more variables are included, as well as language recognition based on url, menu/module/template/widgets etc. setting based on url and menu id. and a database object connection.


Model.php
Code:
<?
	class Model{
		
		private $system = NULL;
		private $content = '';
		
		public function __construct($system){
			$this->system = $system;
			echo 'Model/system/id: '.$this->system->id.'<br><br>';
			
			$this->content = $this->set_content();
		}

		private function set_content() {
			require_once('Content.php');
				
			$content_obj = new Content($this->system);
			$content = $content_obj->get_content();
			
			return $content;	
		}

		public function get() {
			$return->content = $this->content;
			return $return;
		}
		
		public function get_id() {
			echo 'Model/get_id/system/id: '.$this->system->id.'<br><br>';
			print_r($this->system);
		}

	}
Note: in the actual framework the model included classes for static texts from xml files, template/module/widget static texts from xml, class for the module and a class for each widget.

Content.php
Code:
<?
	class Content extends Model{
		private $system = NULL;
		
		public function __construct($system){
			$this->system = $system;
			$content = $this->set_content();
			
			echo 'Content/system/id: '.$this->system->id.'<br><br>';
		}

		private function set_content() {
			$id = parent::get_id();
			
			echo 'Content/set_content/id: '.$id.'<br><br>';
			
			return $id;		
			
		}
	}
this is a simplified example of a widget, renamed to "content" for the purpose of this topic.



The specific issue I am having between this one and the model class is the "parent::get_id()" part.
this is returning blank. The entire class run as above returns the following:

Model/system/id: 3
Model/get_id/system/id:
Content/set_content/id:
Content/system/id: 3

if in the get_id function in model i put "print_r($this)" it shows the following:
Code:
Content Object ( [system:Content:private] => stdClass Object ( [id] => 3 ) [system:Model:private] => [content:Model:private] => )

however if i put "print_r($this->system)" it does not print out anything. I am having trouble understanding why.


I would also like to know if there is a better way to pass the system object to the underlying class instead of passing it with the new class call each time and storing a copy inside each class.

Overall any comments in the way i have structured this framework would be greatly appreciated. I played with a few mvc frameworks and tutorials but none of them really do exactly what I want. I also feel that making it from scratch is a good learning experience :)

Thank you
Thomas




site | blog | iphones |
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top