I'd like to know if there is an alternative to "this" and "super" from Java in PHP.

Greets, K?!

Recommended Answers

This is supported in php. I'm not to sure if the keyword super is. Here's a simple example of this:

class Human 
{
	public $thirsty = 'Very thirsty!';
	
	function drink($water)
	{
		$this->thirsty = 'Not thirsty!';
	}	
}
Jump to Post

The super keyword, to my understanding would be used with this syntax in php

parent::someFunc();
Jump to Post

All 5 Replies

I don't know javascript to well what are this and super used for.

This is supported in php. I'm not to sure if the keyword super is. Here's a simple example of this:

class Human 
{
	public $thirsty = 'Very thirsty!';
	
	function drink($water)
	{
		$this->thirsty = 'Not thirsty!';
	}	
}

The super keyword, to my understanding would be used with this syntax in php

parent::someFunc();

Thank you :-).
Greets, K?!

Edit: Synking, i don't know if javascript uses this and super, but i meant java.
"This" and "super" are used to another variable then the one you would use normally. For example, when you have 3 variables with the same name in a superclass, a class an a function of the class, "super" will refer to the variable in the superclass, "this" will refer to the variable in the class, and use of none will refer to the closest variable, in this case the one in the function itself.

php uses the keyword this but does not use super. also, parent::method() works like super().method(). When accessing any methods or variables for php inside of the class you must reference using the this keyword as such

class test{
   public $var
   public function tester($x){
       $this->var=$x;
  }
}

when accessing methods or variables outside the class you must also use -> instead of . so in java you would use

test var=new test();
var.tester();

php would be

$var= new test();
$var->tester();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.