Visibility and invariants
Lesson 33 of 48 in Coddy's PHP for JavaScript Developers course.
JavaScript #private fields enforce privacy; an underscore alone does not. PHP private is a visibility modifier on a declared member. Port the access boundary deliberately: #field syntax is not valid PHP and a public property with an underscore does not recreate it.
private members can be accessed only by the declaring class. Expose operations that preserve invariants instead of letting callers write every field. A getter can expose a scalar snapshot safely, but returning a mutable object may expose shared state. Visibility is an enforced boundary, not documentation alone.
JavaScript program
class Secret {#value="sealed"; reveal(){return this.#value;}} console.log(new Secret().reveal());Source output:
sealedPHP program
demo.php
<?php
declare(strict_types=1);
class Secret {private string $value="sealed";public function reveal():string{return $this->value;}} echo (new Secret())->reveal(),"\n";
PHP output:
sealedChallenge
EasyInput is a list of integer changes. Thermostat begins at 20. set(int $value):bool accepts inclusive 10..30, rejecting others without state change. temperature():int reads the private state.
The supplied main script reads one JSON value from standard input, checks the named public APIs, and prints the result. Implement the module declarations without printing; do not replace the launcher or read input from a module. Input satisfies the stated types and bounds.
Edit Thermostat.php. main.php is the locked entry file. Dependencies use require_once __DIR__ with a relative filename.
Try it yourself
<?php
declare(strict_types=1);
require_once __DIR__."/Thermostat.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
function thermostatReport(array $i):int{$t=new Thermostat();foreach($i as $n)$t->set($n);return $t->temperature();}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
$t=new Thermostat();ensure(!$t->set(9)&&$t->temperature()===20,"private invariant");ensure($t->set(30)&&!$t->set(31)&&$t->temperature()===30,"boundary");
echo (string)thermostatReport($input),"\n";
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in PHP for JavaScript Developers
1Values and output
Modules and first runFormatting returned textAssignments and variablesDivision and conversionStrings and byte lengthsExplicit Boolean rulesRecap: receipt text4Functions and modules
Functions and return typesDefaults and named callsVariadic argumentsCallbacks and capturesrequire_once and scopeRecap: integer cents7Object contracts
Visibility and invariantsStatic methods and factoriesInheritance and overridingInterfaces are nominalComposition and injectionRecap: selectable policy2Control flow
Branches and precedenceStrict match expressionsLoops and early exitsKeys and iterationRecap: shipping rules5Errors and boundaries
Exceptions and stateStrict input validationNull is not falsyRecap: validate a batchPractice on your own: Online PHP compiler