Page 1 of 1
PEAR Coding Standard: Class, Method and Variable naming
Posted: Wed Mar 17, 2010 10:48 am
by jeff00seattle
Hi
I am a little confused about PEAR coding standards pertaining the proper naming of:
- Classes
- Methods
- Variables
I looked at
PEAR Coding Standard and ran
PHP_CodeSniffer using PEAR standard.
By habit from years of C++ and C# coding, I still prefer to use a combination of
camel-case and
hungarian notation. For example:
- class FooBar
- public function fooBar()
- $strFooBar = "My foo bar";
Running
PHP_CodeSniffer, it did not complain about my coding labeling.
However, would my code pass review by PHP purest using my coding style?
Thanks
Jeff in Seattle
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Wed Mar 17, 2010 2:39 pm
by Christopher
Because PHP apps are separated into many loadable scripts, class names are namespaced with underscores to make autoloading simple. It is essentially what the linker would do for compiled languages. So the class name would be Foo_Bar.
I understand that you still prefer C++ and C# coding styles.

Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 12:13 am
by jeff00seattle
arborint wrote:Because PHP apps are separated into many loadable scripts, class names are namespaced with underscores to make autoloading simple. It is essentially what the linker would do for compiled languages. So the class name would be Foo_Bar.
Sorry, I do not understand why would
adding underscores to class names makes autoloading simpler?
Thanks for your reply,
Jeff in Seattle
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 1:49 am
by Benjamin

Moved to PHP - Theory and Design
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 2:37 am
by Christopher
jeff00seattle wrote:Sorry, I do not understand why would adding underscores to class names makes autoloading simpler?
This is the basic idea that most frameworks and libraries use:
Code: Select all
<?php
function __autoload($class_name) {
require_once str_replace('_', '/', $class_name) . '.php';
}
It allows you to organize your scripts into sub-directories to keep things organized. So class Foo_Bar is in file Foo/Bar.php.
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 4:06 am
by Weirdan
jeff00seattle wrote:
By habit from years of C++ and C# coding, I still prefer to use a combination of
camel-case and
hungarian notation
However, would my code pass review by PHP purest using my coding style?
There's nothing wrong with FooBar as a class name per se, however if you have FooBar, FooFoo and FooKungFu with similar interfaces they should have been, most likely, named Foo_Bar, Foo_Foo and Foo_KungFu to keep the files they reside in in a common folder named Foo:
Opinions on hungarian notation tend to vary wildly - some people swear by it, some - hate it. Some, like Joel Spolski, would say you use it wrong (
http://www.joelonsoftware.com/articles/Wrong.html, scroll to "I'm Hungary" section).
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 10:52 am
by jeff00seattle
Weirdan wrote:Opinions on hungarian notation tend to vary wildly - some people swear by it, some - hate it.
Some, like Joel Spolski, would say you use it wrong (http://www.joelonsoftware.com/articles/Wrong.html, scroll to "I'm Hungary" section) .
I am not sure if Joel Spolski's article was either FOR or AGAINST Hungarian-Notation. The article seemed to me as neutral and presenting a historical commentary.
True, most some of my variables do not strictly follow
Hungarian-notation.
For example, labeling variables that are strings:
- sz Pointer to first character of a zero terminated string.
- st Pointer to a string. First byte is the count of characters cch.
- p Pointer to an instance.
Since I write code that will be used cross-platforms (Windows and Linux), using an Hungarian-Like-Notation:
- str Pointer to any string value.
- o Pointer to an object.
Be that as it may,
Would PHP Influencers and PEAR Purest frown if my variables were named using Camel-Case + Hungarian-Notation?
Thanks for your reply
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 11:33 am
by Weirdan
jeff00seattle wrote:Weirdan wrote:Opinions on hungarian notation tend to vary wildly - some people swear by it, some - hate it.
Some, like Joel Spolski, would say you use it wrong (http://www.joelonsoftware.com/articles/Wrong.html, scroll to "I'm Hungary" section) .
I am not sure if Joel Spolski's article was either FOR or AGAINST Hungarian-Notation.
He's for hungarian notation, but thinks putting variable type into the variable name is stupid (however contradicting it sounds to you). Basically what I gather from that article is that programmers misunderstood the original idea of hungarian notation. Meaning what you use and what is commonly called Hungarian notation now (useless noise IMO) is an absolutely different from what it was originally (useful notation to distinguish and visually group variables serving similar purposes, directly comparable, assignment-compatible in a semantic sense, etc).
jeff00seattle wrote:Be that as it may, Would PHP Influencers and PEAR Purest frown if my variables were named using Camel-Case + Hungarian-Notation?
Thanks for your reply
I'm would, for sure, but I don't think my opinion in any way representative.
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Thu Mar 18, 2010 4:55 pm
by Christopher
jeff00seattle wrote:True, most some of my variables do not strictly follow
Hungarian-notation.
But it is the style you like, for what that is worth.
jeff00seattle wrote:For example, labeling variables that are strings:
- sz Pointer to first character of a zero terminated string.
- st Pointer to a string. First byte is the count of characters cch.
- p Pointer to an instance.
Since I write code that will be used cross-platforms (Windows and Linux), using an Hungarian-Like-Notation:
- str Pointer to any string value.
- o Pointer to an object.
PHP does not have pointers on either Windows or Linux, so that would be cross-platform unnecessary.
jeff00seattle wrote:Be that as it may, Would PHP Influencers and PEAR Purest frown if my variables were named using Camel-Case + Hungarian-Notation?
Camel Case no. Hungarian yes. Variable type annotations in names yes.
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Fri Mar 19, 2010 12:54 am
by jeff00seattle
#1. What is the result after creating an instance of a Class using new?
Agreed, PHP does not have address pointers in the sense of C++; however, calling new returns an instance of an object in the heap.
So, following the Hungarian-notation, what would be in PHP the most appropriate prefix to a variable to indicate it contains an instance of a Class?
#2. No camel-case and Yes hungarian-notation
Not OK?
Yes OK?
Thanks for the feedback
Jeff in Seattle
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Fri Mar 19, 2010 1:59 am
by Christopher
jeff00seattle wrote:Agreed, PHP does not have address pointers in the sense of C++; however, calling new returns an instance of an object in the heap.
It returns what they call a handle as of PHP5. Thinking of it as a pointer or pondering how or where it is stored will only get you in trouble in PHP. It's more magical like a file handle than a pointer.
jeff00seattle wrote:So, following the Hungarian-notation, what would be in PHP the most appropriate prefix to a variable to indicate it contains an instance of a Class?
I think most PHP programmers name object something objecty like $person and strings something stringy like $name. Variable type is not strict in PHP so all variables are either objects, strings or arrays. Numbers of various sorts get converted back and forth to/from strings on the fly depending on context.
jeff00seattle wrote:#2. No camel-case and Yes hungarian-notation
Not OK?
Yes OK?
Either is fine with me, just be consistent. You don't need the "str" on either because, as I said, all scalars are essentially strings.
Re: PEAR Coding Standard: Class, Method and Variable naming
Posted: Fri Mar 19, 2010 9:55 am
by jeff00seattle
This mentoring has been great!
Thanks!