Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword class

Declare an object type with its own properties and methods.

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

Syntax

class Counter { public int $value = 0; }

Declare an object type with its own properties and methods. Each new instance has its own nonstatic property values. Assigning an existing object to another variable copies its object handle, so both variables can observe mutations of the same instance.

class Counter { public int $value=0; public function add():void {++$this->value;} }
$a=new Counter(); $b=$a; $a->add(); $c=new Counter(); echo json_encode([$b->value,$c->value,$a===$b]);
[1,0,true]

Examples

<?php
class Counter { public int $value=0; public function add():void {++$this->value;} }
$a=new Counter(); $b=$a; $a->add(); $c=new Counter(); echo json_encode([$b->value,$c->value,$a===$b]);

Result

[1,0,true]

Sources