2

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. 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 shown above: this.x = x;

Eugene
  • 4,352
  • 8
  • 55
  • 79
  • 2
    see http://stackoverflow.com/questions/577575/using-the-keyword-this-in-java – user187291 Dec 04 '10 at 14:34
  • So as I see it is very different from PHP `this` keyword usage. Or better to say syntax? – Eugene Dec 04 '10 at 14:51
  • possible duplicate of [What is going on here in PHP with classes?](http://stackoverflow.com/questions/3845769/what-is-going-on-here-in-php-with-classes) – user207421 Dec 05 '10 at 00:26

3 Answers3

4

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.

Dmitri
  • 34,780
  • 9
  • 39
  • 55
  • 2
    I always include the `this` keyword for clarity. I in fact would prefer if the compiler generated an error if I failed to include it. Code should be clear. – Erick Robertson Dec 04 '10 at 15:50
3

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

Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53
  • Except, that if I had attributes `a` and `b`, then I could easily avoid `this` usage in constructor and assign `a = x` and `b = y` without `this`. Can't say same about PHP. – Eugene Dec 04 '10 at 14:53
  • 2
    In PHP you cannot omit the keyword `$this` in any case. And in my oppinion you shouldnt omit it in java also. – KingCrunch Dec 04 '10 at 15:15
0

Java and PHP are different the way they handle the this keyword.

Read this question and answer, and it explains some odd behaviour of the this keyword in PHP.

Community
  • 1
  • 1
Codemwnci
  • 54,176
  • 10
  • 96
  • 129