{"id":2203,"date":"2021-11-14T21:11:19","date_gmt":"2021-11-14T18:11:19","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=2203"},"modified":"2021-11-14T21:12:50","modified_gmt":"2021-11-14T18:12:50","slug":"__invoke-magic-method-in-php","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/","title":{"rendered":"__invoke() Magic Method in PHP"},"content":{"rendered":"\n<p>There are various <strong>magic methods<\/strong> that will make our work easier in PHP. Magic methods begin with two underscores(__construct, __invoke). In this post, I will explain the <strong>__invoke()<\/strong> magic method and its usage, which is frequently used in PHP.<\/p>\n\n\n\n<h3>__invoke() :<\/h3>\n\n\n\n<p>The <strong>__invoke<\/strong> is a method that we define in the class. If we call the object as a function after instantiating an object from the class, the method to be called is the __invoke() method.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>First we will define a class named <strong>Car<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">class Car{\n\n    public function __construct(){\n        echo &quot;object created &lt;br&gt;&quot;;\n    }\n\n    public function __invoke(){\n        echo &quot;invoke method called&quot;;\n    }\n}<\/pre><\/div>\n\n\n\n<p>Let&#8217;s create an object of this class. When we create an object, the<strong> __construct<\/strong> method will run. If we then call the object as a function, the <strong>__invoke<\/strong> method will run.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$obj = new Car();\n\n$obj();\n\n\/\/Output: object created \n\/\/Output: invoke method called <\/pre><\/div>\n\n\n\n<p>We can check whether the object is callable with the <strong>is_callable()<\/strong> function before calling it.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">echo is_callable($obj) ? 'yes' : 'no';\n\/\/Output : yes <\/pre><\/div>\n\n\n\n<h3>Callable Typehint<\/h3>\n\n\n\n<p>The<strong> callable typehint <\/strong>checks whether the thing being called is callable or not so that it is executed. It came with <strong>PHP 5.4.<\/strong> Below you will see a usage of how to use the <strong>__invoke magic<\/strong> method.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">function start_engine(Callable $func) {\n    $func();\n    return &quot;engine started&quot;;\n  }\n\n$obj = new Car();\n\necho start_engine($obj);\n\/\/Output: object created \n\/\/Output: invoke method called\n\/\/Output: engine started<\/pre><\/div>\n\n\n\n<p>Good Luck &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are various magic methods that will make our work easier in PHP. Magic methods begin with two underscores(__construct, __invoke). In this post, I will explain the __invoke() magic method and its usage, which is frequently used in PHP. __invoke() : The __invoke is a method that we define in the class. If we call [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>__invoke() Magic Method in PHP - blog website<\/title>\n<meta name=\"description\" content=\"There are various magic methods that will make our work easier in PHP. Magic methods begin with two underscores(__construct, __invoke). In this post, I will explain the __invoke() magic method and its usage, which is frequently used in PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"__invoke() Magic Method in PHP - blog website\" \/>\n<meta property=\"og:description\" content=\"There are various magic methods that will make our work easier in PHP. Magic methods begin with two underscores(__construct, __invoke). In this post, I will explain the __invoke() magic method and its usage, which is frequently used in PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-14T18:11:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-14T18:12:50+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/\",\"name\":\"__invoke() Magic Method in PHP - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"datePublished\":\"2021-11-14T18:11:19+00:00\",\"dateModified\":\"2021-11-14T18:12:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"There are various magic methods that will make our work easier in PHP. Magic methods begin with two underscores(__construct, __invoke). In this post, I will explain the __invoke() magic method and its usage, which is frequently used in PHP.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/11\/14\/__invoke-magic-method-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"__invoke() Magic Method in PHP\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2203"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=2203"}],"version-history":[{"count":15,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2203\/revisions"}],"predecessor-version":[{"id":2228,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2203\/revisions\/2228"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=2203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=2203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=2203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}