[thelist] another php question: object reference syntax

Jackson Yee jyee at vt.edu
Wed Jul 17 21:53:01 CDT 2002


----- Original Message -----
From: <Jonathan_A_McPherson at rl.gov>
To: <thelist at lists.evolt.org>
Sent: Wednesday, July 17, 2002 18:02
Subject: RE: [thelist] another php question: object reference syntax


> Many programmers (myself included) actually consider the mandatory $this->
> to be a desirable feature. Many "newer" languages force you to specify class
> scope with a "this" pointer when you're in a method, so that there is no
> risk of confusion between local method scope and class scope.

Matters of coding style always involve some compromise between some elements
and other elements.  Being required to specify object scope has the potential
to increase readability in long functions, but for shorter functions, it can
actually degrade readability and increase the amount of work on the developer.
 Consider the following trivial example for a rectangle class in PHP:

Without class scoping, the code would look like this:

class Rect
{
  var $Left;
  var $Top;
  var $Right;
  var $Bottom;

  function Rect()
  {
    $Left = 0;
    $Top = 0;
    $Right = 0;
    $Bottom = 0;
  }

  function GetWidth()
  {
    return $Right - $Left;
  }

  function GetHeight()
  {
    return $Bottom - $Top;
  }

  function GetArea()
  {
    return GetWidth() * GetHeight();
  }
}

With required class scoping, the code would instead look like this:

class Rect
{
  var $Left;
  var $Top;
  var $Right;
  var $Bottom;

  function Rect()
  {
    $this->Left = 0;
    $this->Top = 0;
    $this->Right = 0;
    $this->Bottom = 0;
  }

  function GetWidth()
  {
    return $this->Right - $this->Left;
  }

  function GetHeight()
  {
    return $this->Bottom - $this->Top;
  }

  function GetArea()
  {
    return $this->GetWidth() * $this->GetHeight();
  }
}

Arguments can be made for either case, but as for myself, the first version is
more concise and easier to read, as my eyes do not have to constantly scan for
and discard the '$this->' on every variable reference.  Note that class
members in C++ and Java can be easily rewritten with 'this->' preceding the
member name with the exact same functionality, but most people don't because
it's not necessary for them.  I have seen Microsoft programmers use the prefix
'm_' for class members quite often for Win32 code, but that again, is a coding
standard and not a requirement.  Don't get me started on Hungarian notation
either.  [shakes head]

My point is that it would be nice if I wasn't required to explicity specify
class scope.  PHP could search both the local and class namespaces when a
variable reference is found in a function definition, and it could make my
coding considerably easier at times.  It's a design decision that I'll live
with though, since I wouldn't choose a scripting language to do large-scale
development work anyway.  8-)

Regards,
Jackson Yee
jyee at vt.edu
http://www.jacksonyee.com/




More information about the thelist mailing list