Hi Guys,
I am wanting to design my own OO PHP framework, assuming a framework is a load of class files which I can interact with to make stuff happen? I am quite competent with standard PHP and I want to get to grips with OO PHP so I think the best thing to do is get stuck in and play with it... However I am getting confused with Model View Control which seems to be the standard now a days with PHP5+. I am finding it very difficult to decide how to divide the project up into classes and I am struggling to find ways to make things reusable, at the current rate I am going to have a load of function etc that only ever get used once in the code... Does anyone know any good guides, books or tutorials on OO PHP architecture, not OO PHP syntax tutorials more the theory of how to professionally develop OO projects. Ideally I would like to follow a project from start to finish to understand why things have been done the way they have, but no Software or Web firms will let me loiter while they develop stuff lol.
MVC Object Orientated PHPMVC? Structuring OO PHP
Page 1 of 1
10 Replies - 5908 Views - Last Post: 06 June 2011 - 10:06 PM
Replies To: MVC Object Orientated PHP
#2
Re: MVC Object Orientated PHP
Posted 24 January 2011 - 12:18 PM
You should take a look at codeIgniter, download it, play with it see how it works. Maybe re-write the code into your own framework to really understand it. Basically an application built using a MVC pattern goes like this:
Your index.php file loads all core classes such as the router class. The router takes information from the URI and loads the proper class/function. For example:
The URI: www.example.com/blog/article
Would be fed through the router class as the class to load being "blog" (the "controller", the C in MVC) and the method being "article". To get the URI string you'd have to have a URI class that handles the URI and authenticates it etc..
Then you'd have a Display class (the "view", the V in MVC) that actually spits the rendered page to the browser, and with caching etc...
The M in MVC, models, are classes that are loaded in your controller for connecting to databases etc..
So it goes like this:
Controller is loaded -> Method of controller is called -> View file (html) is loaded and echoed to the browser.
Here is a very simple example application outline:
index.php
- Define application globals and include paths
- Load core classes (base controller class that all application classes will extend), Router class, URI class, and Display class.
- Run the Router class (which also calls the URI class to fetch the URI string etc.)
- Get the controller name from the router class (if none defined use default controller)
- Get the method name from the Router class (if none, call the index() method (all controllers are required to have an index method for default calls))
- Call the class and method Controller_name:method_name();
so if your URI looked like this 'www.example.com/blog/article' then you would have a controller named blog and a method named article. The blog controller would look like this:
Your base controller class would load all extra classes like a 'Loader' class which could be used to load certain files like addons or other classes. The Loader class would then be available to all your sub Controllers so you can do something like
To load the Display library into your controller;
I don't know if anything I said makes sense to you or not but that is sort of how it goes, any questions?
Your index.php file loads all core classes such as the router class. The router takes information from the URI and loads the proper class/function. For example:
The URI: www.example.com/blog/article
Would be fed through the router class as the class to load being "blog" (the "controller", the C in MVC) and the method being "article". To get the URI string you'd have to have a URI class that handles the URI and authenticates it etc..
Then you'd have a Display class (the "view", the V in MVC) that actually spits the rendered page to the browser, and with caching etc...
The M in MVC, models, are classes that are loaded in your controller for connecting to databases etc..
So it goes like this:
Controller is loaded -> Method of controller is called -> View file (html) is loaded and echoed to the browser.
Here is a very simple example application outline:
index.php
- Define application globals and include paths
- Load core classes (base controller class that all application classes will extend), Router class, URI class, and Display class.
- Run the Router class (which also calls the URI class to fetch the URI string etc.)
- Get the controller name from the router class (if none defined use default controller)
- Get the method name from the Router class (if none, call the index() method (all controllers are required to have an index method for default calls))
- Call the class and method Controller_name:method_name();
so if your URI looked like this 'www.example.com/blog/article' then you would have a controller named blog and a method named article. The blog controller would look like this:
<?php
class Blog extends Controller{
function __construct(){
//Run the parent class constructor
parent::__construct();
}
//index method required by all controllers
function index(){
//Show default page or something here
}
function article(){
//Show an article page or whatnot here (load the article view file (HTML))
}
}
?>
Your base controller class would load all extra classes like a 'Loader' class which could be used to load certain files like addons or other classes. The Loader class would then be available to all your sub Controllers so you can do something like
$this->loader->library('Display');
To load the Display library into your controller;
I don't know if anything I said makes sense to you or not but that is sort of how it goes, any questions?
This post has been edited by Xtron: 24 January 2011 - 12:20 PM
#3
Re: MVC Object Orientated PHP
Posted 26 January 2011 - 06:59 AM
I have a question about the model aspect of MVC. Would it be a good practice to use the model to store functions of commonly used code for a certain features on a site?
For a site I am working on, I use a number of custom functions for each system on my site. I am not sure if it's good practice to store these functions in the models that utilize the table or if I should create something like a helper function?
An example would be my user system. I want to be able to set the users to be their own object when created (IE sender and recipient/player and opponent/friend and enemy). These objects will hold functions that will effect that player's database information (IE giving currency to the player, sending player a notice, checking the user's inventory for a certain item). I have been debating with myself as of late if I should store all of these inside of the model, or create a helper that use the model to pull of those actions.
For a site I am working on, I use a number of custom functions for each system on my site. I am not sure if it's good practice to store these functions in the models that utilize the table or if I should create something like a helper function?
An example would be my user system. I want to be able to set the users to be their own object when created (IE sender and recipient/player and opponent/friend and enemy). These objects will hold functions that will effect that player's database information (IE giving currency to the player, sending player a notice, checking the user's inventory for a certain item). I have been debating with myself as of late if I should store all of these inside of the model, or create a helper that use the model to pull of those actions.
#4
Re: MVC Object Orientated PHP
Posted 26 January 2011 - 10:11 AM
Helper functions do not have any object orientated code to them, they are strictly functions that complete their own tasks.
For a user type system that you described you would have your controller call methods from a model that get user data from the database. Like this:
Controller:
-- Load a Database object (that when is loaded, connects to a database if it hasn't already, and becomes available to your model)
-- Load the model that contains the functions for getting user data and such
You can create a User library instead of putting them all in your controller.
User_Controller.php (The class that will handle everything like output through a Display class)
User_Model.php (For getting user data out of the database)
User.php (The actual user functionality)
Sending a "player notice" would be a function in your User_Model.php file because it would be inserting something into a database.
So to answer your question, no helper files aren't really made for that purpose, they are just static functions that perform an independent task basically.
Hope this helps =]
For a user type system that you described you would have your controller call methods from a model that get user data from the database. Like this:
Controller:
-- Load a Database object (that when is loaded, connects to a database if it hasn't already, and becomes available to your model)
-- Load the model that contains the functions for getting user data and such
You can create a User library instead of putting them all in your controller.
User_Controller.php (The class that will handle everything like output through a Display class)
User_Model.php (For getting user data out of the database)
User.php (The actual user functionality)
Sending a "player notice" would be a function in your User_Model.php file because it would be inserting something into a database.
So to answer your question, no helper files aren't really made for that purpose, they are just static functions that perform an independent task basically.
Hope this helps =]
#5 Guest_Mark S*
Re: MVC Object Orientated PHP
Posted 01 February 2011 - 09:34 AM
Pretty much every "MVC" framework in PHP is actually closer to 3-tier.
Most people (and frameworks!) make 2 fundamental mistakes in applying the MVC pattern in PHP:
1) They give the controller model level responsibility so it's fetching records, doing validation and other processing See: http://blog.astrumfu...ppreciated.html ( the site seems to be down at the moment, see google's cache: http://webcache.goog...-unappreciated/ )
2) The give the controller responsibility over its view and give the controller responsibility of passing model data to the view (in mvc the view requests its own data from the model) See: http://r.je/views-ar...-templates.html
Most people (and frameworks!) make 2 fundamental mistakes in applying the MVC pattern in PHP:
1) They give the controller model level responsibility so it's fetching records, doing validation and other processing See: http://blog.astrumfu...ppreciated.html ( the site seems to be down at the moment, see google's cache: http://webcache.goog...-unappreciated/ )
2) The give the controller responsibility over its view and give the controller responsibility of passing model data to the view (in mvc the view requests its own data from the model) See: http://r.je/views-ar...-templates.html
#6
Re: MVC Object Orientated PHP
Posted 22 March 2011 - 02:26 PM
I have been writing a cms frame that implements a MVC like design pattern.
It's not an exact MVC design pattern but I had to put a personal twist on it and made it a little more easier for me to get use to.
check it out here:
Cribz Cms Framework MVC
It's not an exact MVC design pattern but I had to put a personal twist on it and made it a little more easier for me to get use to.
check it out here:
Cribz Cms Framework MVC
This post has been edited by chtombleson: 22 March 2011 - 02:27 PM
#7
Re: MVC Object Orientated PHP
Posted 05 June 2011 - 12:15 PM
Why would you invent the weel for the 400th time? There are loads and loads of good and easy-to-use php frameworks with a good documentation.
I can suggest CakePHP, I already made various websites with it and I'm very happy off it
I can suggest CakePHP, I already made various websites with it and I'm very happy off it
#8
Re: MVC Object Orientated PHP
Posted 05 June 2011 - 12:28 PM
shadowstep0705, on 05 June 2011 - 03:15 PM, said:
Why would you invent the weel for the 400th time?
- To learn. The best way to learn is by doing.
- Because maybe you'll come up with something new and revolutionary. That's been the history of computers: somebody builds their own version of something and in the process develops something new and better than what came before.
#9
Re: MVC Object Orientated PHP
Posted 06 June 2011 - 07:36 AM
CTphpnwb, on 05 June 2011 - 12:28 PM, said:
shadowstep0705, on 05 June 2011 - 03:15 PM, said:
Why would you invent the weel for the 400th time?
- To learn. The best way to learn is by doing.
- Because maybe you'll come up with something new and revolutionary. That's been the history of computers: somebody builds their own version of something and in the process develops something new and better than what came before.
Ok, right, to learn I agree. But if it's for a whole website with login/shop functions and stuff, I think it's better to use an existing one, because you will always have more security leaks then a framework developed by professional for 4 years now
#10
Re: MVC Object Orientated PHP
Posted 06 June 2011 - 08:00 PM
A framework might be more secure. On the other hand, if somebody found a hole while hacking another site that used the same framework then they might be able to use the same vulnerability against your site. That would make using the framework less secure.
#11
Re: MVC Object Orientated PHP
Posted 06 June 2011 - 10:06 PM
CTphpnwb, on 07 June 2011 - 04:00 AM, said:
A framework might be more secure. On the other hand, if somebody found a hole while hacking another site that used the same framework then they might be able to use the same vulnerability against your site. That would make using the framework less secure.
That's why the popular frameworks have almost weekly updates
And the user doesn't know what framework you're using
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote






|