open(); if ($file instanceof OpenFile) { $content = $file->read(); $file->close(); } elseif ($file instanceof CannotOpen) { // Mooo. } for ($i = 1; $i < 10; ++$i) { // TODO: What here? $file->open(); } // ClosedFile $file = new MyFile("somefile"); if ($file instanceof OpenFile) { // TODO: Impossible } if (hardcomputation()) { $file->open(); } // $file is OpenFile|ClosedFile|CannotOpen // TODO: Have to check state? /** * @param OpenFile $file */ function readall(OpenFile $file) { $file->read(); $file->close(); // TODO: Not allowed to change state of aliased variable. return $file; // TODO: Not allowed to transfer ownership } /** * @param string $name * @return ClosedFile */ function makefile($name) { // ClosedFile $file = new File($name); dosomething($file); // Can't change state, still closed return $file; // Owner can change } /** * @param ClosedFile $file * @param-out OpenFile $file */ function open(ClosedFile &$file) { }