{"id":1770,"date":"2021-06-23T22:56:31","date_gmt":"2021-06-23T19:56:31","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=1770"},"modified":"2021-06-23T22:59:03","modified_gmt":"2021-06-23T19:59:03","slug":"magic-constants-in-php","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/","title":{"rendered":"Magic Constants in PHP"},"content":{"rendered":"\n<p><strong>PHP<\/strong> has predefined constants that come with various <strong>extensions<\/strong>. Knowing these constants allows us to use PHP more effectively.<\/p>\n\n\n\n<h4>1.) <strong><code>__LINE__<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the line number where it is used.<\/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;:true,&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 &quot;Line number is : &quot;. __LINE__;\n\n\/\/OUTPUT: Line number is : 2<\/pre><\/div>\n\n\n\n<h4>2.) <strong><code>__FILE__<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the file path and filename.<\/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;:true,&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 &quot;File path : &quot;. __FILE__;\n\n\/\/OUTPUT: File path : C:\\Bitnami\\wampstack-7.3.25-0\\apache2\\htdocs\\learn-lara\\app\\Http\\Controllers\\FrontController.php<\/pre><\/div>\n\n\n\n<h4>3.) <strong><code>__DIR__<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the directory where the file is located.<\/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;:true,&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 &quot;Folder path : &quot;. __FILE__;\n\n\/\/OUTPUT: Folder path : C:\\Bitnami\\wampstack-7.3.25-0\\apache2\\htdocs\\learn-lara\\app\\Http\\Controllers<\/pre><\/div>\n\n\n\n<h4>4.) <strong><code>__FUNCTION__<\/code><\/strong><\/h4>\n\n\n\n<p>When used inside a function, it returns the name of the function it is in.<\/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;:true,&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;}\">public function myFunction()\n    {\n  \t\techo &quot;Function name : &quot;. __FUNCTION__;\n\t}\n\n\/\/OUTPUT: Function name : myFunction<\/pre><\/div>\n\n\n\n<h4>5.) <strong><code>__CLASS__<\/code><\/strong><\/h4>\n\n\n\n<p>When used inside a class, it returns the name of the class it is in. It is also used in traits.<\/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;:true,&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 FooBar\n{\n    public function getClassName(){\n        return __CLASS__;\n    }\n}\n$obj = new FooBar();\necho $obj-&gt;getClassName();\n\n\/\/OUTPUT: FooBar<\/pre><\/div>\n\n\n\n<h4>6.) <strong><code>__TRAIT__<\/code><\/strong><\/h4>\n\n\n\n<p>When used inside a trait, it returns the name of the trait it is in.<\/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;:true,&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;}\">trait FooBarTrait{  \n    function myTraitFunction(){  \n        echo __TRAIT__;  \n        }  \n    }  \n    class Student{  \n        use FooBarTrait;  \n        }  \n    $a = new Company;  \n    $a-&gt;myTraitFunction(); \n\n\/\/OUTPUT: FooBarTrait<\/pre><\/div>\n\n\n\n<h4>7.) <strong><code>__METHOD__<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the name of the method in which it is used.<\/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;:true,&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 Student\n{\n    public function myClassMethod(){\n        return __METHOD__;\n    }\n}\n$obj = new Student();\necho  $obj-&gt;myClassMethod();\n\n\/\/OUTPUT: myClassMethod<\/pre><\/div>\n\n\n\n<h4>8.) <strong><code>__NAMESPACE__<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the current namespace.<\/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;:true,&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;}\">namespace FooBarNamespace;\n  \nclass Student {\n    public function myMethod() {\n        return __NAMESPACE__;\n    }\n}\n  \n$obj = new Company();\necho  $obj-&gt;myMethod(); \n  \n\/\/OUTPUT: FooBarNamespace<\/pre><\/div>\n\n\n\n<h4>9.) <strong><code>ClassName::class<\/code><\/strong><\/h4>\n\n\n\n<p>Returns the fully qualified class name in which it is used.<\/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;:true,&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;}\">namespace FooBarNamespace;\nclass Student{ }\n  \necho Geeks::class;\n\n\/\/OUTPUT: FooBarNamespace\\Student<\/pre><\/div>\n\n\n\n<p>Good luck&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP has predefined constants that come with various extensions. Knowing these constants allows us to use PHP more effectively. 1.) __LINE__ Returns the line number where it is used. 2.) __FILE__ Returns the file path and filename. 3.) __DIR__ Returns the directory where the file is located. 4.) __FUNCTION__ When used inside a function, it [&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>Magic Constants in PHP - blog website<\/title>\n<meta name=\"description\" content=\"Magic Constants in PHPPHP has predefined constants that come with various extensions. Knowing these constants allows us to use PHP more effectively.\" \/>\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\/06\/23\/magic-constants-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magic Constants in PHP - blog website\" \/>\n<meta property=\"og:description\" content=\"Magic Constants in PHPPHP has predefined constants that come with various extensions. Knowing these constants allows us to use PHP more effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-23T19:56:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-23T19:59:03+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\/06\/23\/magic-constants-in-php\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/\",\"name\":\"Magic Constants in PHP - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"datePublished\":\"2021-06-23T19:56:31+00:00\",\"dateModified\":\"2021-06-23T19:59:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"Magic Constants in PHPPHP has predefined constants that come with various extensions. Knowing these constants allows us to use PHP more effectively.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/06\/23\/magic-constants-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magic Constants 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\/1770"}],"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=1770"}],"version-history":[{"count":41,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1770\/revisions"}],"predecessor-version":[{"id":1815,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1770\/revisions\/1815"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}