...

View Full Version : Please give me some advice on PHP coding style.



abduraooft
09-05-2007, 01:14 PM
Hello,
I'd like to get some help/advice on the way of writing PHP codes.

Generally I write a page like
1)Include a template page which may have the other include directives of class-files, functions for adding doctype, <head> section, page heading, nav-bar, footer etc.
2)Each page may include some specific class-files if any.
3)Call the functions from the template wherever required and call other functions from class-files according to the required flow execution.
4)No html part is generated inside class-file, rather it'll be added in the other files along with the data obtained from the class-files if any.
5)Don't use same page to display contents with no logical relation with the help of if-else statements.
6)Finally the html part is separated from PHP AFAP.

For me, its very easy to maintain the site.
Now my problem is, it's very hard to customise free packages like phpBB, Joomla etc since they all follow a different style of coding. I know they are doing in the right way because they all provide an easy way to configure the setup even for one who don't know anything about PHP.

Are you all doing similar to them? Is there any resource to learn a style like that? I'm still a beginner, so please give your valuable suggestions.

regards,
art

rafiki
09-05-2007, 01:20 PM
code in your own style, if thats how you find it easier to code do it like that.

Lightro
09-05-2007, 01:24 PM
Yes I have developed my own style in coding, and have found that a creative mind is the way to go.

abduraooft
09-05-2007, 05:18 PM
OK, thank you both for quick replies.
Actually I came up with this thread is on behalf my previous unanswered thread regarding ExtCalendar package (http://www.codingforums.com/showthread.php?t=122917)
I hope you may have seen how the thread regarding php-logo has been solved, and as you all know it's not the right way to solve this problem(http://www.codingforums.com/showthread.php?t=122936)

I can't simply give up this thread without getting some introduction on the above mentioned style of coding as our Active membership count is 8,496 ;) .

regards,
art.

aedrin
09-05-2007, 05:30 PM
code in your own style, if thats how you find it easier to code do it like that.

I found this somewhat amusing.

Q: How do I do X?
A: Do it your own way!

To answer the topic; sites like phpBB and Joomla don't necessarily use the right way. I've found that with most "big" open source packages the source code is rather convoluted and hard to read. Not to mention bloated. I certainly wouldn't use them to measure your code quality with.

You have some good methods of coding going, but there is always room for improvement.

The main thing that I find helps me is Object Oriented design, but in a more basic way. I tend to model the basic, grand scheme of things. For instance, I have a class that handles displaying a page in a certain way, reading certain files, etc. I have a few that handle the page differently, but they all do the same thing (rendering HTML to present to the user).


2)Each page may include some specific class-files if any.

I forgot where I read about this, but this is my setup. Every class I create has its own source file. The way I set up the naming of the class and the files, means I never have to include any source files, because I use __autoload() (http://us3.php.net/__autoload).

For instance, a database class for MySQL would be called Database_MySQL, and it would be found in: Database/MySQL.php (notice how the _ gets translated to a /)

All I have to do in code is: $db = new Database_MySQL(); and the autoloading feature will automatically include my class file.


6)Finally the html part is separated from PHP AFAP.

Remember that your goal is to seperate business logic from presentation logic, not HTML from PHP. This is a common misconception, which has led to templating "methods" such as Smarty.

PHP -is- a templating language. That's what it originally was, and it still is. There is no reason to introduce some esoteric language that you have to learn in addition to PHP.

Business logic should be seperate from presentation. So that you may modify the output without changing the quality of the output.


Are you all doing similar to them? Is there any resource to learn a style like that?

There is 1 resource that will give you the most effective coding solution: your experience.

The more you use PHP, the more you will understand it. The more you understand it, the more efficient your code gets.

There is not 1 right way.

abduraooft
09-05-2007, 06:14 PM
Aah... great relief! thanks a lot.


Remember that your goal is to seperate business logic from presentation logic, not HTML from PHP. This is a common misconception, which has led to templating "methods" such as Smarty.

Could you please explain a little more on your point on Smarty. Is it a bad one?
(I don't know much about this and haven't used)

aedrin
09-05-2007, 09:51 PM
It's not necessarily bad. It's a good idea, but in the end it is just another layer over PHP with nearly the same syntax as PHP. While the whole point was to prevent people from writing PHP.

There are some good uses for it, but I've never found one that required me to use it. Usually it is easier to create something yourself.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum