Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Method Or Property?

<?php

$object = new X();

$one =     $object->foo();   // X::foo

$two = new $object->foo();   // object Y {}

class X {
    public $foo = Y::class;
    
    function foo() {
        return __METHOD__;
    }
}
class Y {}
  
?>

The first line is a simple call to the method foo, on an object whose class is not show here: if this works, there is a method foo() in that class.

The second line is a simple call to the property foo, on an object whose class is not know (again): it has a property called foo. Then, the result of that property, hopefully a string with a class name, or another object, is instantiated, without any arguments.

The nuance is subtle.

See Also

PHP Features

Last updated: 10 September 2026