Jump to content

Recommended Posts

Hi.

 

First a little background on myself:

I've been coding PHP for a number of years (in fact I worked as a programmer part-time while in college writing PHP applications, although it was used by a small number of people internally). I find that my code isn't very professional, for example I'll have a functions.php and just dump all my functions there, or I'll intertwine HTML with PHP, etc. I've worked with classes a bit, but only the basics.

 

I haven't coded for about half a year, but I'm about to start a project that I want to make as professional as possible (to save myself time and to be proud of my code).

I wasn't able to find any articles, etc. specifically related to this. What do you guys suggest? I was thinking of using a framework such as CodeIgniter. Is this a good idea?

 

Input would be much appreicated!

Link to comment
https://forums.phpfreaks.com/topic/195444-how-to-code-in-a-professional-manner/
Share on other sites

Standards are important so that people can easily re-use your code without having to decipher a different language. You may want to start at a site like this

 

http://www.dagbladet.no/development/phpcodingstandard/

 

to get a feel for some standards. Also make sure that your code is well documented every step of the way. Where you are creating functions you should document what should go into the function and what should come out of it.

Great link, thanks!

 

What are your views on using a framework such as the one I mentioned in my original post? I guess it will make it easier to stick to the principles described in your link. Also, what about a templating system? Should they be used in all but the simplest of projects to separate code from HTML?

Great link, thanks!

 

What are your views on using a framework such as the one I mentioned in my original post? I guess it will make it easier to stick to the principles described in your link. Also, what about a templating system? Should they be used in all but the simplest of projects to separate code from HTML?

 

Remember. Being consistent is very important. So while I believe these should be up to you, you should do it that way always or never. Not half and half ;)

You are at a point that many PHP developers hit, and many (most) have a hard time getting past.  PHP is so easy to figure out a solution to any problem, and the flexibility often leads to bad practices and poor habits.

 

Here are some of the best advice I can give, to break past this plateau!

 

#1 - use an MVC framework.  If you don't know what MVC is, do some research..it basically means you are separating your business logic (Model...or M), and your presentation (View, or V).  The Controller © is sort of the middleman.  It grabs the data, and pushes it to a view :)  I HIGHLY recommend Kohana for a PHP framework if you are comfortable with OOP practices.  A framework may feel limiting at first, but it forces best practice down your neck...also, Kohana allows infinite flexibility, so if you feel you can't achieve something - I promise you're just doing it wrong :)

 

#2 - Read codeigniters php style guide

http://codeigniter.com/user_guide/general/styleguide.html

 

This is a great resource, and will put some things into perspective on how your code should LOOK :)

 

 

#3 - NAMING CONVENTIONS

This is so important when you start to work on enterprise level applications.  Be diligent in naming things correctly (Frameworks will force this on you), and take time to think of the right way TO name a class, function, or even variable. 

 

#4 - Along with #3, NAMESPACING!

Use PHP's autoload method.  Instead of a functions.php, put your functions into a class and call them statically.  This will let you use autoload, and group your functions into more digestible pieces.  Autoloading means only those functions/classes that you need access to are being included, and will reduce your overall "include bloat".  Autoload is the bees knees.

 

#5 - Read other peoples code.  Download open source projects, and see how they do it.  You will learn a TREMENDOUS amount this way...both in how to write code, and how to approach heavy duty business logic.

 

Good luck!  Be diligent, and always refactor...you learn the most by doing, and spending time to make your code better (after it already works) will make you want to do it right the first time.

 

 

Great link, thanks!

 

What are your views on using a framework such as the one I mentioned in my original post? I guess it will make it easier to stick to the principles described in your link. Also, what about a templating system? Should they be used in all but the simplest of projects to separate code from HTML?

 

Frameworks are a good idea when writing production, "I have a project for a client and need to get results" code.  There's really no good reason not to use one as they greatly increase productivity without impacting your own creativity.  It's nice not having to write yet more database connection code, or yet another set of form validation statements.  Seeing how frameworks work is also a good way to learn how professionals code, not just in terms of style, but with their technique as well.  CodeIgniter is a good, popular choice.  You may also want to check out the Zend Framework and Kohana (the PHP 5 version of CodeIgniter).

 

Rolling your own has value, too, but mostly in terms of expanding your own knowledge.  Learn why things work, experiment and dabble, but when it's time to pay the bills you'll be happy you chose a framework.

 

I don't recommend using a template engine at all, regardless of the project.  Native PHP is more than suited for the job, and doesn't incur the overhead of a 3rd party template system.

Wow thanks for the excellent replies guys!

 

Just to confirm, when you say don't use a templating system, are you also referring to the built-in templating systems most of these frameworks have?

 

I'm still not 100% sure of which framework is best. I've done a bit of research and the contenders seem to be cakePHP, CodeIgniter, Zend, Symphony and Kohana. I was going to go for CodeIgniter but since both of you are so ardent in respect to Kohana I might reconsider.

Im surprised no one has mentioned it, but PHPDoc is a life saver. I work on a team of 4 developers, and sometimes the docs are all I have to go off of when trying to amend there work (or vice versa). Also, when you create a great app, and you have no issues for 2 years, and then you finally get a call about it and have to fix it, sometimes you need your own docs :)

 

http://www.phpdoc.org/

I would suggest codeigniter to get yourself started.  Google "nettuts codeigniter from scratch" and you will see some BRILLIANT tutorials.  :)  when you get comforable with OOP and Codeigniter I would suggest Kohana then.  Codeigniters documentation and community is superior, but Kohana is the better framework (in my opinion)

 

 

Protip: If you do use Kohana, be sure to use mysqli in the database configuration if you're using a MySQL database.  There's a bug in the low-level system code that stops the more familiar mysql option from working.  It's not hard to fix, but I wouldn't recommend diving into the system files unless you know what you're looking for.  It's a simple isset issue, but it's buried in the system.  Hopefully they'll fix it in the next release.

 

Other than that, I have had no complaints with Kohana so far.  It's stupidly simple to use.

There are really three big wins for Kohana

 

#1)  Autoloading of classes is HUGE.  You never have to include files, or "load" a class (like in codeigniter).  This goes past the libraries and into the helpers as well.  All the helpers are contained in classes, and every method can be called statically.  I know it doesn't SOUND like a huge benefit, but it really encourages you to abstract a lot of your business logic out of your models and into their own library.  This also helps with namespacing a TONNN.  This is a bad example but in Kohana you could call these two functions

 

user::get_data();

auto::get_data();

 

not so big on small projects, but on huge ones sometimes it's hard to keep things generic, and also keep namespace collisons from happening.

 

#2)  Cascading file system.  If a controller isn't available in your application directory, it looks for a module.  No module?  Checks your system directory.  This allows you to reuse a ton of logic across multiple applications/modules in your system.  Super cool.

 

#3)  I know people will disagree here, but the ORM.  Codeigniter doesn't have an ORM built in, Kohana does.  You can shoehorn Doctrine into either frameworks...but honestly, Kohanas built in ORM rocks!  Yes, ORM is not as effiient as raw SQL...but for MANY(most) parts of an application it honestly doesn't matter.  The increase in development time, and the simpliity of code makes it a godsend.  We handle 30,000 concurrent users...use database for our session control AND use orm extensively.  The performance hit is neglible if you take some basic precautions.

 

Oh, and Kohana is fully OOP and php5.0+ only.  This turns some people off, but it's part of why I love the framework so much.  More focus on staying cutting edge let's me develop in a framework that takes advantage of PHPs little known maturity.  Check it out - if you have any questions I'll be happy to help!

I was on the fence between CI and Kohana, but I decided to go with CI for now. I see the huge community & great documentation as a big boon. I don't want to waste a lot of time figuring out relatively trivial things (especially since I'm an OO & MVC newbie). Perhaps after I gain a bit of experience I'll move on to Kohana.

Thanks again for the replies, they were exactly what I needed.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.