this keyword in Java and in PHP

Today I started work on a small Java app. I have some experience with PHP OOP and mostly the principle is one and the same. Though I thought, that it should apply both ways.

But for instance keyword this is used differently as I understand. For instance in Java

class Params
{
    public int x;
    public int y;

    public Params( int x, int y )
    {
        this.x = x;
        this.y = y;
    }

    public void display()
    {
        System.out.println( "x = " + x );
        System.out.println( "y = " + y );
    }
}

public class Main
{
    public static void main( String[] args )
    {
        Params param = new Params( 4, 5 );
        param.display();
    }
}

At the same time in PHP it is needed to make same thing

<?php

class Params
{
    public $x;
    public $y;

    public function __construct( $x, $y )
    {
        $this->x = $x;
        $this->y = $y;
    }

    public void display()
    {
        echo "x = " . $this->x . "\n";
        echo "y = " . $this->y . "\n";
    }
}

class Main
{
    public function __construct()
    {
        $param = new Params( 4, 5 );
        $param->display();
    }
}

$main = new Main();

?>

I just would like to ask are there some other differences in this keyword?

Since I see, that in Java it is used to return instance of modified object and if I pass argument with same name, as atribute in the class. Then to assign value I need to distinctly show what is argument and what is class attribute. For example as show abow: this.x = x;

Answers


In Java you don't always need to say 'this' Java will figure it out. The only situation when you need to say this is when the local variable is the same name as instance variable, in which case Java will use local variable if you don't say this.var

But you still can say this.var even when it's not necessary in Java if it makes you understand the code better.


Yes , "this" keyword in php works the same as in java & there is no other difference


Need Your Help

How do I prevent the empty instance from appearing in the view?

ruby-on-rails ruby-on-rails-3

I have topics_controller.rb that creates a new @reply instance variable for the inline form in the show page.

Finding maximum difference between power functions

r statistics

How do I estimate the maximum difference between the power functions?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.