PHP 8.5.0 Alpha 2 available for testing

Voting

: five plus zero?
(Example: nine)

The Note You're Voting On

jimmmy dot chief at gmail dot com
8 years ago
Hi, i would like to point out difference between self::CONST and $this::CONST with extended class.
Let us have class a:

<?php
class a {
const
CONST_INT = 10;

public function
getSelf(){
return
self::CONST_INT;
}

public function
getThis(){
return
$this::CONST_INT;
}
}
?>

And class b (which extends a)

<?php
class b extends a {
const
CONST_INT = 20;

public function
getSelf(){
return
parent::getSelf();
}

public function
getThis(){
return
parent::getThis();
}
}
?>

Both classes have same named constant CONST_INT.
When child call method in parent class, there is different output between self and $this usage.

<?php
$b
= new b();

print_r($b->getSelf()); //10
print_r($b->getThis()); //20

?>

<< Back to user notes page

To Top