CKTIS / PHP
Kľúčové slovo private
Obmedzuje prístup na deklarujúcu triedu.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
private int $balance = 0;Obmedzuje prístup na deklarujúcu triedu. Jej metóda môže čítať súkromné pole inej inštancie tej istej triedy. Potomok môže deklarovať vlastné súkromné pole rovnakého mena. Priamy prístup zvonka bez magickej metódy vyvolá 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
Príklady
<?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);}
Výsledok
true Error