CKTIS / PHP
Keyword private
Restrict member access to the declaring class.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
private int $balance = 0;Restrict member access to the declaring class. Its methods can access private fields on another instance of that same class. A child may declare a separate private property with the same name. Direct outside access to a private property without a magic handler raises Error.
class Account {public function __construct(private int $balance){} public function same(self $b):bool{return $this->balance===$b->balance;}}
$a=new Account(7); echo json_encode($a->same(new Account(7))); try {echo $a->balance;} catch (Error $e) {echo " ",get_class($e);}
true Error
Examples
<?php
class Account {public function __construct(private int $balance){} public function same(self $b):bool{return $this->balance===$b->balance;}}
$a=new Account(7); echo json_encode($a->same(new Account(7))); try {echo $a->balance;} catch (Error $e) {echo " ",get_class($e);}
Result
true Error