php / php/doc-en

Exception::$message can't be extends with string type

Open
#2,232 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description
Documentation book

The PHP book describes Exception class as follows:
https://www.php.net/manual/en/class.exception.php

class Exception implements Throwable {
/* Properties */
protected string $message= "";

[...]
}
Extending it:

https://3v4l.org/QtEA0#v8.2.1

<?php
class AException extends \Exception
{
    protected $message = 'New Default Message';
}

class BException extends \Exception
{
    protected string $message = 'New Default Message';
}

Resulted in this output:

Fatal error: Type of BException::$message must not be defined (as in class Exception)

But I expected this output instead:
No error. Or an error for class A, not class B.

PHP Version

PHP 8.2.1

Operating System

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Exception class page linked in the issue and reproduce the PHP 8.2.1 examples to confirm the documented property type and inheritance behavior. Done means the documentation accurately explains the behavior and the examples no longer contradict the observed result.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.