Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between "Java Constructor" and "Php Constructor".

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 397
    Comment on it

    Hello reader's
    Today we will discuss about "Difference between Java Constructor and Php Constructor".

    Within Java we are able to override the constructors within one class. Constructor is recognized as function getting same label in particular class. You can also find a couple of constructors! That is certainly a big difference between java and PHP. As PHP class may have merely one constructor function __construct(). Usually, program may fail when there is more than one constructor in php.


    Difference involving a couple class constructors is actually parameter. Therefore Java look at these two functionality not similar. Many function dependent differently on their construction, not according to his function name. Therefore we can effortlessly produce functionality having very same function name, but appearing in a different way dependent on their constructor (incoming parameters). This kind of actions is named polymorphism.


    In PHP we don't have a pair of operates possessing exact same function name along with possessing different parameter of functions. So in the event we try to write a pair of operates along with exact same function name inside PHP class, but with different function name.

    Java constructor example

    /**
    * 
    * 
    * @auther Jaydeep Dhoundiyal
    * @return void
    */
    public class consTest {
    
        /*Default Constructor*/
        public consTest(){
            System.out.println("Hello I am default constructor.");
        }
        /*Constructor with one parameter*/
        public consTest(String name){
            System.out.println("Hi I am ."+name);
        }
      public static void main(String[] args) 
      {        
        System.out.print("Hi there!\n");
    
        consTest obj1 = new consTest();
    
        consTest obj2 = new consTest("NOt default");
    
      }    
    
    }
    


    Php example for constructor

    <?php
    
    class consTest {
    
      private $name = '';
     //php Constructor
      public function __construct ()
      {
        $this->name = 'Php constructor';
      }
    
    
      public function getName ( )
      {
        return $this->name;
      }
    
      public function consTest ( )
      {
         return 'I am not constructor. I am a method';
      }
    
    }
    
    $call = new consTest();
    echo $call->getName();
    // display: Php constructor
    echo $call->consTest();
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: