J. Mike Rollins (Sparky) [rollins@wfu.edu]
  CISSP, GIAC GPEN
Hyperbola New
My Cats New
Kitty New
Mike is on
  LinkedIn
  FaceBook
BackYardGreen.Net
HappyPiDay.com
Green Cycle Design Group
CamoTruck.Net
  Resume  
  My Stuff  
  Art  
  My Truck  
  People  
Electronics
Jacob's Ladder
Scripts
Math
Notes
   MySQL FK
   smb
   MIME
   PHP/Perl Reference
   SQL update
   Base64
My House
My Cars
My Cats New
My Jokes
Pi Poetry
pumpkin
Toro Mower
Development
Speed of a Piston
Not a Pipe
Linux
















PHP/Perl Reference

I am proficient with Perl. More and more, PHP is used in many of the applications I use (Gallery2 and PHPBB2). My first real PHP program was an XML parser that used recursion and objects. I spent hours debugging the code. A better understanding of PHP references would have saved me much time.

The following code defines an object using Perl and PHP.

    Perl
    PHP
    package MyTest;
    
    use strict;
    
    sub new
      {
        my $pkg = shift;
    
        my $obj = {
            'value' => 0,
            'is_null' => 1
          };
        bless $obj,$pkg;
    
        return $obj;
      }
    
    sub setValue
      {
        my $obj = shift;
        my $value = shift;
    
        $obj->{'value'} = $value;
        $obj->{'is_null'} = 0;
      }
    
    sub getValue
      {
        my $obj = shift;
        return $obj->{'value'};
      }
    
    sub isNull
      {
        my $obj = shift;
        return $obj->{'is_null'};
      }
    
    return 1;
    
    <?php
    
    class MyTest
      {
        var $value;
        var $is_null;
    
        function MyTest()
          {
            $this->value = 0;
            $this->is_null = true;
          }
    
        function setValue($value)
          {
            $this->value = $value;
            $this->is_null = false;
          }
    
        function getValue()
          {
            return $this->value;
          }
    
        function isNull()
          {
            return $this->is_null;
          }
    
        }
    ?>
    
    



Part 1

A Perl script an a PHP script will illustrate a subtle difference in the two languages.
    Perl
    PHP
    #!/usr/bin/perl
    
    use MyTest;
    
    #
    # Create new instance of MyTest()
    #
    
    my $a = new MyTest();
    
    $a->setValue(10);
    
    print "Create new object \$a\n";
    print "A = " . $a->getValue() . "\n\n";
    
    #
    # Assign b to a
    #
    
    my $b = $a;
    
    print "Assign \$b = \$a\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    #
    # Setting b to 11
    #
    
    print "Setting \$b to 11\n";
    $b->setValue(11);
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    exit(0);
    
    <?php
    
    require 'MyTest.php';
    
    /*
     * Create new instance of MyTest()
     */
    
    $a = new MyTest();
    
    $a->setValue(10);
    
    print "Create new object \$a\n";
    print "A = " . $a->getValue() . "\n\n";
    
    /*
     * Assign b to a
     */
    
    $b = $a;
    
    print "Assign \$b = \$a\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    /*
     * Setting b to 11
     */
    
    $b->setValue(11);
    print "Setting \$b to 11\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    ?>
    
The output of these scripts show a major difference between Perl and PHP.
    Perl
    PHP
    Create new object $a
    A = 10
    
    Assign $b = $a
    A = 10
    B = 10
    
    Setting $b to 11
    A = 11
    B = 11
    
    Create new object $a
    A = 10
    
    Assign $b = $a
    A = 10
    B = 10
    
    Setting $b to 11
    A = 10
    B = 11
    



Part 2

PHP performs a "copy" of the object in response to the = operator. Perl creates a reference to the existing object. The =& operator will instruct PHP to create a reference to the existing object. But, the relationship with the reference is much stronger that in Perl.

The following modifies the PHP code from the previous example:

    Perl
    PHP
    #!/usr/bin/perl
    
    use MyTest;
    
    #
    # Create new instance of MyTest()
    #
    
    my $a = new MyTest();
    
    $a->setValue(10);
    
    print "Create new object \$a\n";
    print "A = " . $a->getValue() . "\n\n";
    
    #
    # Assign b to a
    #
    
    my $b = $a;
    
    print "Assign \$b = \$a\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    #
    # Setting b to 11
    #
    
    print "Setting \$b to 11\n";
    $b->setValue(11);
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    exit(0);
    
    <?php
    
    require 'MyTest.php';
    
    /*
     * Create new instance of MyTest()
     */
    
    $a = new MyTest();
    
    $a->setValue(10);
    
    print "Create new object \$a\n";
    print "A = " . $a->getValue() . "\n\n";
    
    /*
     * Assign b to a
     */
    
    $b =& $a;
    
    print "Assign \$b =& \$a\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    /*
     * Setting b to 11
     */
    
    $b->setValue(11);
    print "Setting \$b to 11\n";
    print "A = " . $a->getValue() . "\n";
    print "B = " . $b->getValue() . "\n\n";
    
    ?>
    
Now the output of these two scripts are the same.
    Perl
    PHP
    Create new object $a
    A = 10
    
    Assign $b = $a
    A = 10
    B = 10
    
    Setting $b to 11
    A = 11
    B = 11
    
    Create new object $a
    A = 10
    
    Assign $b =& $a
    A = 10
    B = 10
    
    Setting $b to 11
    A = 11
    B = 11
    



Part 3

Be careful, the =& operator creates a stronger association with the object than the = operator in Perl.
    Perl
    PHP
    #
    # Add the following to the Perl script
    #
    
    $a = 0;
    print "Setting \$a=0\n";
    print "A = " . $a . "\n";
    print "B = " . $b->getValue() . "\n";
    
    #
    # This will display
    #
    # Setting $a=0
    # A = 0
    # B = 11
    #
    
    /*
     * Add the following code to the php script
     */
    
    $a = 0;
    print "Setting \$a=0\n";
    print "A = " . $a . "\n";
    print "B = " . $b->getValue() . "\n";
    
    /*
     * This will produce the following
     *
     * Setting $a=0
     * A = 0
     * Fatal error:  Call to a member function on a non-object...
     *
     */
    
The assignment of $a=0 in the PHP code replaced the object that $a and $b referenced with the number 0. So, $b now references the number 0.