{"id":9718,"date":"2021-03-22T11:00:59","date_gmt":"2021-03-22T02:00:59","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9718"},"modified":"2021-03-22T11:52:30","modified_gmt":"2021-03-22T02:52:30","slug":"magic-methods-in-php-part-2","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9718","title":{"rendered":"Magic methods in PHP &#8211; Part 2"},"content":{"rendered":"\n<p>I will continue to talk about magic methods article I wrote last week. For the part 1, you can read <a href=\"\/appnews\/archives\/9672\">here<\/a>.<\/p>\n\n\n\n<p>ToString<\/p>\n\n\n\n<p>The magic __toString () method allows you to define what you would like to display when an object of the class is treated as a string. If you use echo or print on your object, and you haven&#8217;t defined the __toString () method, it will give an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Student {\n    private $name;\n \n    public function __construct($name) \n    {\n        $this->name = $name;\n        $this->email = $email;\n    }\n \n    public function __toString()\n    {\n        return 'Name: '.$this->name;\n    }\n}\n \n$student = new Student('Yuuma');\necho $student;\n?><\/code><\/pre>\n\n\n\n<p>Call<\/p>\n\n\n\n<p>If the __get () and __set () methods are called for non-existent properties, the __call () method is called when trying to invoke inaccessible methods, the methods that have not been defined in your class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Student {\n    public function __call($methodName, $arg) \n    {\n        \/\/ $methodName = getName\n        \/\/ $arg = array('1')\n    }\n}\n \n$student = new Student();\n$student->getName(1);\n?><\/code><\/pre>\n\n\n\n<p>Isset<\/p>\n\n\n\n<p>The magic __isset () method is called when you call the isset () method on inaccessible or non-existent object properties. Let&#8217;s see how it works with an example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Student {\n    private $data = array();\n \n    public function __isset($name) \n    {\n        return isset($this->data[$name]);\n    }\n}\n \n$student = new Student();\necho isset($student->email);\n?><\/code><\/pre>\n\n\n\n<p>Invoke<\/p>\n\n\n\n<p>The magic __invoke () method is a special method that is called when you try to call an object as if it were a function. the $student object is treated as if it were a function, and since we have defined the __invoke () method, it will be called instead of giving you an error. The main purpose of the __invoke () method is that if you want to treat your objects as callable, you can implement this method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Student {\n    private $name;\n \n    public function __construct($name) \n    {\n        $this->name = $name;\n    }\n \n    public function __invoke()\n    {\n        echo 'Invoke function';\n    }\n}\n \n$student = new Student('John');\n$student();\n?><\/code><\/pre>\n\n\n\n<p>There are still some magic methods in PHP but let me stop here as I had written most used methods in article part 1 and part 2. If you are interest for more, you can check it out in PHP official <a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.magic.php\">documentation<\/a>.<\/p>\n\n\n\n<p>Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9718\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9718\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Magic methods in PHP &#8211; Part 2\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9718\" data-text=\"Magic methods in PHP &#8211; Part 2\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9718\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9718\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>I will continue to talk about magic methods article I wrote last week. For the part 1, you can read here. ToSt [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9718"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9718"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9718\/revisions"}],"predecessor-version":[{"id":9724,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9718\/revisions\/9724"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}