Skip to content

[Bug] __invoke and __call magic methods are not supported #182

@nahime0

Description

@nahime0

Description

Two important magic methods are not properly supported:

  1. __invoke — Objects implementing __invoke cannot be called using direct call syntax ($obj(42)).
  2. __call — Calls to undefined methods on an object that defines __call are not intercepted and instead produce "Undefined method" errors.

Reproduction

__invoke

class CallableObj {
    public function __invoke($x) { return $x * 2; }
}

$obj = new CallableObj();
echo $obj(21);   // Should call __invoke

__call

class Proxy {
    public function __call($method, $args) {
        return "called:$method:" . implode(',', $args);
    }
}

$p = new Proxy();
echo $p->doSomething(1, 2, 3);

Expected behavior

  • $obj(21) should invoke __invoke and return 42
  • $p->doSomething(1,2,3) should return "called:doSomething:1,2,3"

Actual behavior

  • __invoke: Cannot call $obj — not a callable (got Object("CallableObj"))
  • __call: Undefined method: CallableObj::foo

Environment

elephc 0.21.8 vs PHP 8.4

Possible root cause

The type checker and call resolution logic do not consider __invoke when determining whether an expression is callable. Similarly, the method resolution for unknown methods does not fall back to __call on the receiver's class.

Relevant areas:

  • src/types/checker/ (callable detection and first-class callable logic)
  • src/types/checker/functions/call_validation.rs
  • src/codegen/expr/calls/

Additional context

Discovered while stress-testing magic methods in Round 2.

These are fundamental PHP object model features used in many libraries and DSLs.

Suggested labels: bug, php-compatibility, type-checker, magic-methods

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmagic-methodsMagic methods (__get, __call, __invoke...)php-compatibilityPHP compatibility / deviation from PHP behaviortype-checkerStatic type checker / inference issues

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions