Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword public

A public member is accessible from outside its class.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

public private(set) int $count = 0;

A public member is accessible from outside its class. In PHP 8.4 a typed property may declare a narrower write visibility separately. A public private(set) property is readable externally, while assignment is restricted to the declaring class. Public methods can perform such assignments.

class Counter {public private(set) int $count=0; public function advance():void{++$this->count;}}
$c=new Counter(); $c->advance(); echo $c->count; try{$c->count=8;}catch(Error $e){echo " ",get_class($e);}
1 Error

Examples

<?php
class Counter {public private(set) int $count=0; public function advance():void{++$this->count;}}
$c=new Counter(); $c->advance(); echo $c->count; try{$c->count=8;}catch(Error $e){echo " ",get_class($e);}

Result

1 Error

Sources