PEAR Coding Standard: Class, Method and Variable naming

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
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

PEAR Coding Standard: Class, Method and Variable naming

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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. ;)
(#10850)
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PEAR Coding Standard: Class, Method and Variable naming

Post by Benjamin »

:arrow: Moved to PHP - Theory and Design
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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.
(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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:

Code: Select all

 
Foo/
  Bar.php
  Foo.php
  KungFu.php
 
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).
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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:
  1. sz Pointer to first character of a zero terminated string.
  2. st Pointer to a string. First byte is the count of characters cch.
  3. p Pointer to an instance.
Since I write code that will be used cross-platforms (Windows and Linux), using an Hungarian-Like-Notation:
  1. str Pointer to any string value.
  2. 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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:
  1. sz Pointer to first character of a zero terminated string.
  2. st Pointer to a string. First byte is the count of characters cch.
  3. p Pointer to an instance.
Since I write code that will be used cross-platforms (Windows and Linux), using an Hungarian-Like-Notation:
  1. str Pointer to any string value.
  2. 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.
(#10850)
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR Coding Standard: Class, Method and Variable naming

Post by jeff00seattle »

#1. What is the result after creating an instance of a Class using new?

Code: Select all

$value = new Foo();
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?

Code: Select all

$strFooBar = "My String Value";
Yes OK?

Code: Select all

$str_foo_bar = "My String Value";
Thanks for the feedback

Jeff in Seattle
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PEAR Coding Standard: Class, Method and Variable naming

Post 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?

Code: Select all

$strFooBar = "My String Value";
Yes OK?

Code: Select all

$str_foo_bar = "My String Value";
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.
(#10850)
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR Coding Standard: Class, Method and Variable naming

Post by jeff00seattle »

This mentoring has been great!

Thanks!
Post Reply