Skip to content

Asymmetric visiblity error when main visibility is omittedΒ #2959

@JoyceBabu

Description

@JoyceBabu

The main visibility can be omitted when it is public. Docs

If a property is public, then the main visibility may be omitted. That is, public private(set) and private(set) will have the same result.

So,

class Book
{
    public function __construct(
        public private(set) string $title,
        public protected(set) string $author,
        protected private(set) int $pubYear,
        public string $bar,
    ) {}

    public function methodThatNowWontParse(): void
    {}
}

can be shortened to

class Book
{
    public function __construct(
        private(set) string $title,
        protected(set) string $author,
        protected private(set) int $pubYear,
        public string $bar,
    ) {}

    public function methodThatNowWontParse(): void
    {}
}

But this triggers an Undefined variable error as given below.

Image

--

The test for this feature contains the following code, which is invalid.

class Book
{
    public function __construct(
        public private(set) string $title,
        public protected(set) string $author,
        protected private(get) int $pubYear,
        public string $bar,
    ) {}

    public function methodThatNowWontParse(): void
    {}
}

protected private(get) int $pubYear fails with error

Fatal error: Multiple access type modifiers are not allowed

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions