Search code examples
phpoopthismethod-call

What does the array payload mean in the context of some_function(array($this, 'something'))


While looking at code online or as part of examples or tutorials, I sometimes see some_function(array($this, 'something')) what does that mean? I've never seen array syntax like that and it really confuses me.

One example is this code from a comment in the manual:

<?php

    class ClassAutoloader {
        public function __construct() {
            spl_autoload_register(array($this, 'loader'));
        }
        private function loader($className) {
            echo 'Trying to load ', $className, ' via ', __METHOD__, "()\n";
            include $className . '.php';
        }
    }

    $autoloader = new ClassAutoloader();

    $obj = new Class1();
    $obj = new Class2();

?>

Can someone please explain what that syntax means?


Solution

  • This is a PHP callable. It means call the method loader on the object $this.

    Here is a link with more information: http://php.net/manual/en/language.types.callable.php