PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 4th, 2012, 11:40 AM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
PHP-OOP - Best practices

Hi guys,

I am making an attempt on learning OOP. Now I thought before I go ahead, does anyone know some best practices. like standards for variable, class and function(method) naming/notation. I have seen quite a few like camel notation and all, but not sure if anyone knows or could point me to a sort of W3C-like recommendations for writing. If anyone has tips or hint let me know
__________________
PHP Tutorial

Reply With Quote
  #2  
Old December 4th, 2012, 12:42 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,690 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
There are no such standards, thankfully. You are free to see the various standards, like Zend's or that one Github (?) project that nominated itself as The Standard To Rule Them All, and adopt the ones or parts of that you like most.

Reply With Quote
  #3  
Old December 4th, 2012, 12:43 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,861 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 18 h 15 m 23 sec
Reputation Power: 813
Hi,

I'm not aware of any "official" code style standard. There are standards for particular frameworks and applications like PEAR or the Zend framework. But PHP itself doesn't seem to have specific naming rules -- which is not surprising, since the internal names themselves are a wild mixture of different traditions.

I'd simply use common sense (names should be descriptive etc.) and the following basic rules:
  • class names start with an uppercase letter and use CamelCase
  • constants are all-uppercase
  • methods start with a lowercase letter and either use camelCase or the underscore_notation
  • the same with attributes

Reply With Quote
  #4  
Old December 4th, 2012, 12:50 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Thanks both of you for the links and advice. I'll have a look at it

Cheers!

P.s. I asked because the tutorial i do doesn't seem very consequent but its fun.
p.p.s funny comic about it

Last edited by aeternus : December 4th, 2012 at 12:57 PM.

Reply With Quote
  #5  
Old December 4th, 2012, 01:12 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,690 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Whatever you choose, the absolute most important thing is consistency. I don't care if you elect to use Hungarian notation so long as you use it consistently. Very few things piss me off more than not knowing whether a class is called, say, ezContentClass or eZContentClass...

Reply With Quote
  #6  
Old December 4th, 2012, 01:16 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Quote:
Originally Posted by requinix
Whatever you choose, the absolute most important thing is consistency. I don't care if you elect to use Hungarian notation so long as you use it consistently. Very few things piss me off more than not knowing whether a class is called, say, ezContentClass or eZContentClass...

Cheers! I'll definitely try be consistent. I pass the Hungarian notation though :P

Reply With Quote
  #7  
Old December 4th, 2012, 04:47 PM
Hall of Famer Hall of Famer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Ithaca
Posts: 64 Hall of Famer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 7 m 48 sec
Reputation Power: 1
Yeah, the best practice is to be consistent with one coding style throughout your project. Also if you wish to use a framework like Zend, Symfony, CakePHP and Codeigniter, its a good practice to follow their standards rather than mixing it up with your own.

Reply With Quote
  #8  
Old December 4th, 2012, 07:06 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 7053
A general trend in the PHP core classes seems to be using underscore notation for functions, and camel case notation for classes and methods. However, as Jacques1 mentioned the core code has a lot of inconsistencies.

I think one of the only standards that is pretty much universally agreed upon is the use of all uppercase for constants. This is pretty much even consistent across programming languages, not just PHP.

To be honest, I would prefer that PHP did have required standards. I work with a lot of diverse code bases on a daily basis and it is honestly extremely annoying to have to switch coding styles every 2 hours because every single project makes up its own.

Consistency is the most important part though, and I agree with that Hall Of Famer said regarding matching the styles of the code you're working on.

Personally I prefer the following:

all identifiers (variable names, function names, class names, class properties, method names, build-in values) except constants are all lowercase with underscores between words
I find it easier to read than camel case. It is syntactically impossible in PHP to not know the difference between a variable name, a function name, a class name and a method name based solely on its context in the code (although this is not true in all languages), so there is no reason to distinguish between them using different styles. This way, it is incredibly simple to remember what format to use for any given identifier and you never accidentally use the wrong format for an identifier, because all identifiers use the same format.

Also, if you name your files according to the code they contain, like most projects do, then you end up with all lowercase file names. This is particularly beneficial when working cross platform, because not all file systems are case sensitive and some version control systems (SVN) have a notoriously bad history of handling mixed filename case.

except constants, because constants are always in uppercase and use underscores to separate words

never use one-character variable names, even for loop count variables
They are a pain in the *** to find/replace on, and that is no exception for loop count variables.

never omit optional curly braces around control statements
You are less likely to make a reading error if you have the braces there. Plus it's a lot easier to go back and add/remove debugging statements. Plus it really does not take long to type two characters.

never use short tags

always indent using hard tabs

I drop my starting and closing brackets to the next line for everything
Code:
if(condition)
{
    code
}

switch(condition)
{
    case: xyz;
        code
        break;
}

function xyz()
{
    code
}

class xyz
{
    code
}

etc.

It's a lot easier to drop/add braces when you can do it by manipulating the entire line.

spacing: I add a space between parameters and operators (except !), but nowhere else
Code:
function(param1, param2, param3);

if($var == 'value')

if(!$var)

No particular reason for this, it's just how I do it.

Anyway; obviously many people will disagree and have different styles, but those are examples of things to consider as you develop your own unique standard.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #9  
Old December 5th, 2012, 01:20 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Quote:
Originally Posted by E-Oreo
A general trend in the PHP [....]

Thanks Alot for the time you took E-Oreo. I see some things in your method I do already. Except for the curly braces. for some reason I do.
PHP Code:
if(){
  
//bla


I will make sure I will be consistent and not invent a $_CHa_oS__nOT_at_ION

Cheers guys, hopefully I'll soon master OOP

Reply With Quote
  #10  
Old December 10th, 2012, 05:52 PM
rstoll rstoll is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 rstoll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m
Reputation Power: 0
Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions

Reply With Quote
  #11  
Old December 10th, 2012, 05:56 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Quote:
Originally Posted by rstoll
Checkout PHP_CodeSniffer. It may help you to be consistent in your code conventions

cheers I'll check it out!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-OOP - Best practices

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap