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

Greets, K?!

Dani AI

Generated

As , and started to show, PHP has the instance pseudo-variable and uses parent:: to reach parent-class behavior. The important extra details for someone coming from Java are the differences around static scope, constructors, and visibility — those are the usual gotchas.

PHP specifics to remember: $this exists only in an object (non‑static) context; calling a non‑static method statically is an error. For static methods/properties use self:: (early/compile‑time binding) or static:: (late static binding — lets child classes override static behavior at runtime). Example showing the difference between self:: and static:::

<?php
class A {
    public static function who(){ echo 'A'; }
    public static function test(){ self::who(); static::who(); }
}
class B extends A {
    public static function who(){ echo 'B'; }
}
B::test(); // outputs: AB

(php.net)

Constructors and parent calls: if a child defines its own __construct() the parent constructor is NOT invoked automatically — call parent::__construct(...) explicitly. parent:: is meant for methods, constants and static properties (e.g. parent::$x), not for magically switching which instance property $this->name refers to. Also note that private members in the parent are not accessible to the child; use protected when the child must read/write a parent field. Small constructor pattern:

<?php
class Base {
    protected $id;
    public function __construct($id){ $this->id = $id; }
}
class Child extends Base {
    public function __construct($id, $extra){
        parent::__construct($id); // explicitly call parent ctor
        $this->extra = $extra;
    }
}

(php.net)

Practical rules when porting Java code: treat $this like Java’s this but avoid it in static methods; replace super.method() with parent::method(); call parent::__construct() when needed; prefer protected (not private) for fields intended for subclasses; use static:: for inheritance-friendly static factories. For authoritative details see the PHP manual links below.

References:
Classes & objects (OOP basics)
Late Static Bindings
Constructors & Destructors
Inheritance / static behavior

Recommended Answers

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 meeting, networking, learning, and sharing knowledge.