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;