Constructors and polymorphism in Java versus PHP

Published:

Add / read comments

From time to time I am playing with Java. I am writing just some small pieces of code, nothing big and complex. Usually I am comparing differences between Java and PHP. I discovered some interesting differences in classes constructing.

Constructors in Java have no return type, not even void. They can take only access modifiers, it means can be public, protected, private. Constructors cannot be abstract, final, native, static. In PHP there is special magic function __construct treated as constructor. But there are significant differences between PHP versions less than 5.3.3 and above 5.3.3. Read more in documentation

First important thing is to get Java runtime environment working on your own machine. It means to install Java virtual machine (JVM).

I am using Eclipse IDE for writing Java programs. For testing / learning purposes I created simple java file with classes inside and put it under package start. Generally I found packages in Java very handy. We can group Java files under packages and every class is then accessible from every file under same package. Also if we need to access class from another package, we just simply import that package, using:

import nameOfPackage.nameOfClass

Or for importing all classess from package, just need to write at the start of file:

import nameOfPackage.*

For testing constructors in Java I wrote following class:

package start;
 
/**
 * @class	TestOne
 */
class TestOne {
 
  /** 
   * @access Private 
   */
  private String name = null;
 
 
  @SuppressWarnings("unused")
  private void __construct(  ) 
  {
    this.name = "default construct";
  }
 
 
  /**
   * Class constructor
   * 
   * @access	Public
   * @param	void
   * @return	void
   */
  public TestOne ( )
  {
    this.name = "default void param test one";	
  }
 
 
  /**
   * Class constructor
   * 
   * @access	Public
   * @param	String	name
   * @return	void
   */	
  public TestOne ( String name )
  {
    if ( name.length() > 0 )
    {
      this.name = name;
    }
  }
 
 
  /**
   * Function return name
   * 
   * @param	void
   * @return	String 
   */
  public String getName ( )
  {
    return this.name;
  }
 
 
  /**
   * Function set name
   * 
   * @param	String	name
   * @return	void 
   */		
  public void setName ( String name )
  {
    if ( name.isEmpty() == false )
    {
      this.name = name;
    }
  }
 
}

So now we have Java class ready. But we need also runable program, which will using (calling) Java class and creating instances of class (using constructors). For this purpose I wrote following program:

package start;
 
public class Instances {
 
  /**
   * 
   * 
   * @param Array	args
   * @return void
   */
  public static void main(String[] args) 
  {		
    System.out.print("Hi there!\n");
 
    TestOne oOne = new TestOne();
 
    System.out.println("Name is: " + oOne.getName() );
 
    TestOne oOneParam = new TestOne("Dubak");
 
    System.out.println("Name is: " + oOneParam.getName() );		
  }	
 
}

As you can see, program is within same package as Java class TestOne, so we have direct access to it from program Instances. So now if I run Java program as Java application in Eclipse IDE, I will get the following result into the console:

Hi there!
Name is: default void param test one
Name is: Dubak 

So what are these differences in class constructors?

In Java we can have more constructors within one class. Constructor is recognized as function having same name as class. In our case, it is public TestOne(). But wait! There are two constructors! And that is big difference between Java and PHP. PHP class can have only one constructor called __construct(). Otherwise, program will fail.

This is example of constructing objects in PHP:

<?php
 
class TestPhp {
 
  private $sName = '';
 
  public function __construct ()
  {
    $this->sName = 'Dubak';
  }
 
 
  public function getName ( )
  {
    return $this->sName;
  }
 
  public function TestPhp ( )
  {
     return 'I am not constructor. Treated as regular method as of PHP 5.3.3';
  }
 
}
 
$oTest = new TestPhp();
echo $oTest->getName();
// display: Dubak
echo $oTest->TestPhp();
// display: I am not constructor. Treated as regular method as of PHP 5.3.3

But those two constructors in Java class are not identical. They can not be, otherwise our compiler would say:

Duplicate method TestOne() in type TestOne

Difference between two class constructors is parameter. So Java consider these two function not identical. They differ based on their structure, not the name. So we can easily create function having same name, but acting differently based on their structure (incoming parameters). This behaviour is called polymorphism.

In PHP we can not have two functions having same name and having different number of parameters. So if we try write two functions with same name within PHP class, but different parameters:


/**
 * Name getter
 * @param void
 * @return String       
 */
public function getName ( )
{
  return $this->sName;
}
 
/**
 * Name getter
 * @param String
 * @return String       
 */
public function getName ( $sParam )
{
   return $this->sName;
}

program will fail:

Fatal error: Cannot redeclare TestPhp::getName() in D:\path\php-script.php on line 27

In PHP there is no polymorphism for creating same object methods acting differently based on parameters unlike Java.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.