PHP __invoke 使用
why they are magic? Because they are magically called by PHP when specific actions happen. The __invoke() method is called when a script tries to call an object as a function. <?php class CallableClass { public function __invoke($x) { var_dump($x); } } $obj = new CallableClass; $obj(5); var_dump(is_callable($obj)); int(5) bool(true) 使用明显的操作方法初始化 例如,当我们有一个提供者时,就会发生这种情况。 aws-sdk-php/src/Endpoint/PatternEndpointProvider.php public function __invoke(array $args = []) { $service = isset($args['service']) ? $args['service'] : ''; $region = isset($args['region']) ? $args['region'] : ''; $keys = ["{$region}/{$service}", "{$region}/*", "*/{$service}", "*/*"]; foreach ($keys as $key) { if (isset($this->patterns[$key])) { return $this->expand( $this->patterns[$key], isset($args['scheme']) ? $args['scheme'] : 'https', $service, $region ); } } return null; } 它使用 invoke 使用一些参数提供端点。我们如何使用这个类? ...