Skip to content

Calling static::method from a non-static method results in a non-static call #1458

@someonewithpc

Description

@someonewithpc

Description

The following code:

<?php

class A
{
    public static function __callStatic(string $method, array $args): void
    {
        echo "Called static '{$method}'\n";
    }

    public function __call(string $method, array $args): void
    {
        echo "Called non static '{$method}'\n";
    }

    public static function static_method(): void
    {
        static::method();
    }

    public function non_static_method(): void
    {
        static::method();
    }
}

A::static_method();           // Output: Called static 'method'
(new A)::static_method();     // Output: Called static 'method'
(new A)->static_method();     // Output: Called static 'method'
(new A)->non_static_method(); // Output: Called non static 'method'

Resulted in this output:

Called static 'method'
Called static 'method'
Called static 'method'
Called non static 'method'

But I expected this output instead:

Called static 'method'
Called static 'method'
Called static 'method'
Called static 'method'

The __call and __callStatic documentation mentions

__call() is triggered when invoking inaccessible methods in an object context.

Which may mean this is intended behaviour, but it is rather confusing.

Essentially, I'd expect the type of method called to be determined by the operator used: :: or ->

PHP Version

8.1.3, 8.1.1

Operating System

Arch Linux and Alpine (docker)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions