Looking for advice on php patterns
Moderator: General Moderators
Looking for advice on php patterns
For an application I am building I would like to have some input on the way I could set up the system. What I've done so far is think about the data I will need. From that I have designed the database tables.
I have a general list of things that I would like to be able to do. Of course I could just start coding all the pages I want to have, using a procedural approach and writing a seperate page for each 'action'.
However, I'm sure that on the long run that's not a good idea. Because the app will change and grow in features a lot, it has to be designed in a modular and flexible way.
The system is one in which users can keep track of their training and climbed routes. They should be able to:
- register/make an account
- log in and make/change their profile
- add, edit and delete training they did and routes they climbed.
- also, visitors of the site should be able to view the routes that have been climbed, in several ways
- probably I'll add a feature that people can choose whether or not their info can be viewed by the public or not
- an admin should be able to edit some of the data, like the users.
I hope the general idea is clear. Now, what I would like to ask is: what approach should I take building an application like this? What patterns would fit here?
I know my question is not very specific, and therefore difficult to answer, but even if people can point me to some other good threads here or articles elsewere, I would be helped. For example, these recent threads were very interesting. But as my knowledge on php patterns is quite limited, I'm not sure what to search for. I've read a bit about MVC but maybe there are some other specific patterns I should take a look at?
I have a general list of things that I would like to be able to do. Of course I could just start coding all the pages I want to have, using a procedural approach and writing a seperate page for each 'action'.
However, I'm sure that on the long run that's not a good idea. Because the app will change and grow in features a lot, it has to be designed in a modular and flexible way.
The system is one in which users can keep track of their training and climbed routes. They should be able to:
- register/make an account
- log in and make/change their profile
- add, edit and delete training they did and routes they climbed.
- also, visitors of the site should be able to view the routes that have been climbed, in several ways
- probably I'll add a feature that people can choose whether or not their info can be viewed by the public or not
- an admin should be able to edit some of the data, like the users.
I hope the general idea is clear. Now, what I would like to ask is: what approach should I take building an application like this? What patterns would fit here?
I know my question is not very specific, and therefore difficult to answer, but even if people can point me to some other good threads here or articles elsewere, I would be helped. For example, these recent threads were very interesting. But as my knowledge on php patterns is quite limited, I'm not sure what to search for. I've read a bit about MVC but maybe there are some other specific patterns I should take a look at?
What kind of feature will you want to add in future ?Because the app will change and grow in features a lot, it has to be designed in a modular and flexible way
I think MVC ( FrontControler or something ) will solve the problem- register/make an account
- log in and make/change their profile
- add, edit and delete training they did and routes they climbed.
- also, visitors of the site should be able to view the routes that have been climbed, in several ways
- probably I'll add a feature that people can choose whether or not their info can be viewed by the public or not
- an admin should be able to edit some of the data, like the users.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Looking for advice on php patterns
If you don't have a solid understanding patterns and MVC, you will not learn them by reading. Just start coding the most important part of the application. You will learn more about the application by coding than thinking. If you want a book, I would recommend Jason Sweat's new book which is a good mix of patterns based and test driven PHP development.matthijs wrote:But as my knowledge on php patterns is quite limited, I'm not sure what to search for. I've read a bit about MVC but maybe there are some other specific patterns I should take a look at?
(#10850)
Thanks for the replies. I'm definately going to study (pardon, experiment with) mvc. I found your example of a front controller Arborint and have tested it. Works fine and looks good. And I do have the book from Jason. However, as you say Arborint, only reading about patterns is not helping me very much (at this moment). Although I understand the patterns fine (the big picture of them, that is) it's difficult to see how and why I should apply which pattern. I guess I'll have to take some smaller steps here and just start coding
...
I'm not asking for someone to decide which pattern to use. I'm just hoping that some advice here, or links to some good discussions or introductionary articles about web application design/ design patterns can speed it up a bit. Or at least prevent me from taking the complete wrong direction.
I'm not asking for someone to decide which pattern to use. I'm just hoping that some advice here, or links to some good discussions or introductionary articles about web application design/ design patterns can speed it up a bit. Or at least prevent me from taking the complete wrong direction.
If I would know I would add it immediately... No serious, what will be added is more data and more possibilities to edit that data. So I'll start easy with the core functionality. At some later point I could add extra's, for example the possibility to add more personal info in the user's profile.What kind of feature will you want to add in future ?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
The problem with patterns is that they are first a language to communicate ideas to implementators. You might be interested to see the orgin of patterns here. They come from architecture and were borrowed by software designers. You just can't do anything complicated until you are a little fluent. So just keep coding and reading about the patterns in parallel.matthijs wrote:I'm not asking for someone to decide which pattern to use. I'm just hoping that some advice here, or links to some good discussions or introductionary articles about web application design/ design patterns can speed it up a bit.
The only way I learn is to go in the complete wrong direction, with conviction, and prove myself wrong. Think of it as eliminating wrong directions.matthijs wrote:Or at least prevent me from taking the complete wrong direction.
(#10850)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
One of hardest things for programmers to accept is that good software design is counter-intuitive. You really have to follow your intuition to truely understand how limiting your designs are -- then stand on the shoulders of giants and learn better ways to design. The whole movement of modern methodologies (OOP/TDD/Agile) is the acknowledgement that the best practices are opposite of the obvious practices (e.g. test first, code last). PHP is like a mad experiment that proves this a thousand times a day because it allows software to be implemented in the obvious way so easily.matthijs wrote:ok, i'll do that: go in the complete wrong direction and learn how not to design first
(#10850)
Talk about "your future features"
.
For example for profile field , you can build an array contain profile field
And everything you do related to user profile , just base on this array 
If I would know I would add it immediately... No serious, what will be added is more data and more possibilities to edit that data. So I'll start easy with the core functionality. At some later point I could add extra's, for example the possibility to add more personal info in the user's profile.
If they're just small feature like new profile field or something small , you just prepare for thatBecause the app will change and grow in features a lot, it has to be designed in a modular and flexible way
For example for profile field , you can build an array contain profile field
Code: Select all
//tablefield => field name ( logic name => business name )
$profile_fields = array(
'name' => 'Fullname' ,
'email' => 'Email address' ,
'phone' => 'Phone number' ,
//etc
);arborint and quocbao, thanks for your feedback. You both bring forward some valid points.
A good practicle example about the profile array quocbao, thanks.
That's an interesting thought. I'll let my thought go over it.One of hardest things for programmers to accept is that good software design is counter-intuitive
A good practicle example about the profile array quocbao, thanks.
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
For me adding a database field is simple. I access a database using a Data Access layer which passes around Transfer Objects representing a single row. The setup is almost identical to ActiveRecord except the Data Access methods for generating SQL are a singleton instance or service which accepts the Transfer Object as an argument to its methods...
Bit like:
Then if the database is changed, I simply re-generate all the Transfer Objects automatically. Hey presto the application is updated without messing with SQL statements, manual Transfer class edits or anything else.
Just a overview - actuals are more complete:
Back to topic...
Patterns are immensely useful but you'll only learn through experience. Believe me there is nothing wrong with coding a PHP application the non-pattern way
. Everyone has done that at least a few times, and it usually lets you see where patterns make sense - that revelation is invaluable when it comes, and it only comes with experience. Doesn't take much by the way, it's not like you need wait a few years or anything.
Patterns are ideas, not implementations - so there are few "perfect" implementations of any pattern in PHP. A lot depends on your own goals, requirements and personal style. There must be dozens of MVC implementations in PHP, all do much the same thing, but get it done in varying ways. Don't lock yourself into one singular solution - keep your mind open and don't be afraid to adapt or innovate.
Bit like:
Then if the database is changed, I simply re-generate all the Transfer Objects automatically. Hey presto the application is updated without messing with SQL statements, manual Transfer class edits or anything else.
Just a overview - actuals are more complete:
Code: Select all
<?php
class TransferObject {
var $dao;
var $class;
var $table;
var $pkey;
var $data = array(); // store row data
function TransferObject($apphelper=false) {
if(!isset($apphelper) || !is_object($apphelper)) {
$this->dao = $apphelper->get('dao');
}
else
{
$this->dao =& DataAccess::getInstance();
}
}
// facade method to DAO
// fetches row data based on primary key value and sets data array to object parameter (this TO)
function getByPk($value) {
$this->dao->getByPk($this, $value);
}
}
class User extends TransferObject {
function User($apphelper) {
parent::TransferObject($apphelper);
// This class is generated - not hand coded.
$this->class = 'User';
$this->table = 'prefix_user';
$this->pkey = 'user_id';
}
function setUserID($val) { $this->data['user_id'] = $val; }
function setUserID($val) { return $this->data['user_id']; }
function setUserName($val) { $this->data['user_name'] = $val; }
function setUserName($val) { return $this->data['user_name']; }
function getPrimaryKeyValue() { return $this->data[ $this->pkey ] }
}
// skip DAO class
//USAGE
$user = new User();
$user->getByPk(1);
echo $user->getUserName();
//UPDATE
$user->setUserName('Maugrim');
$user->save();
//CREATE
$user = new User();
$user->setUserName('feyd');
$user->save();
?>Patterns are immensely useful but you'll only learn through experience. Believe me there is nothing wrong with coding a PHP application the non-pattern way
Patterns are ideas, not implementations - so there are few "perfect" implementations of any pattern in PHP. A lot depends on your own goals, requirements and personal style. There must be dozens of MVC implementations in PHP, all do much the same thing, but get it done in varying ways. Don't lock yourself into one singular solution - keep your mind open and don't be afraid to adapt or innovate.
One suggestion on a good book for patterns.
http://www.amazon.com/gp/product/159059 ... e&n=283155
Very good reading.
http://www.amazon.com/gp/product/159059 ... e&n=283155
Very good reading.
Thanks for your input Maugrim, realy appreciated. Your overview makes sense. And I understand your point about learning patterns. Arborint mentioned it as well. I'll start with the basics. But since this is a personal project, I have the time. So I might as well take a bit of time and use it to start learning some more advanced coding. I definately don't want to code 30+ pages with mysql queries all over the place, so that when the time comes to change something I would have to rewrite them all..
So I'm thinking about starting with some kind of data access layer like you gave, and a front controller like the one arborint gave here. I'll keep things small to start with.
Thanks for the book suggestion jmut, i'll see if my cc company will let me buy another one
So I'm thinking about starting with some kind of data access layer like you gave, and a front controller like the one arborint gave here. I'll keep things small to start with.
Thanks for the book suggestion jmut, i'll see if my cc company will let me buy another one
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland