Looking for advice on php patterns

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
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Looking for advice on php patterns

Post by matthijs »

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?
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Because the app will change and grow in features a lot, it has to be designed in a modular and flexible way
What kind of feature will you want to add in future ?
- 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 think MVC ( FrontControler or something ) will solve the problem :)
User avatar
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

Post by Christopher »

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?
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.
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

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.
What kind of feature will you want to add in future ?
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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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 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:Or at least prevent me from taking the complete wrong direction.
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. ;)
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

ok, i'll do that: go in the complete wrong direction and learn how not to design first :D

I have a feeling that shouldn't be too hard ...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

matthijs wrote:ok, i'll do that: go in the complete wrong direction and learn how not to design first :D
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.
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Talk about "your future features"
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.
Because the app will change and grow in features a lot, it has to be designed in a modular and flexible way
If they're just small feature like new profile field or something small , you just prepare for that :) .

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
);
And everything you do related to user profile , just base on this array :D
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

arborint and quocbao, thanks for your feedback. You both bring forward some valid points.
One of hardest things for programmers to accept is that good software design is counter-intuitive
That's an interesting thought. I'll let my thought go over it.

A good practicle example about the profile array quocbao, thanks.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

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:

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();

?>
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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

One suggestion on a good book for patterns.
http://www.amazon.com/gp/product/159059 ... e&n=283155

Very good reading.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

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 :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I have that book too. It's not too bad, but the examples are very obscure in later chapters. The first 70% of the book is well worth reading.
Post Reply