2017-06-29 1:07 GMT+02:00 David Rodrigues <david.pro...@gmail.com>:
>   readonly $number = mt_rand();
> I agree that "readonly" is a better keyword, but we have to points here:
> 1. "final" is used on some language (Java, C++ I guess);
> 2. "final" keyword does exists on PHP with a "similar" behaviour;

> If we can implements "readonly" is good too, because turn it more implicit.
True we could re-use the final keyword, but think about it this way,
we got final methods and final classes which means it cannot be
overridden/extended respectively, if the final keyword then would also
apply to variables but it would mean they could not be written to,
that would create a WTF-factor.

The readonly keyword would work for any visibility modifiers, so
inherited classes or extending classes may read a protected property,
but not modify it as well:

class A {
 protected readonly $b;

 public function __construct() {
  $this->b = 'C';
 }
}

class B extends A {
 public function read() {
  echo $this->b;
 }

 public function write() {
  $this->b = 'D';
 }
}

$b = new B;

$b->read(); // "C"
$b->write(); // <- error


-- 
regards,

Kalle Sommer Nielsen
ka...@php.net

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to