(window.webpackJsonp=window.webpackJsonp||[]).push([[2475],{2883:function(t,s,a){"use strict";a.r(s);var e=a(31),n=Object(e.a)({},(function(){var t=this,s=t.$createElement,a=t._self._c||s;return a("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[a("h1",{attrs:{id:"magic-constants"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#magic-constants"}},[t._v("#")]),t._v(" Magic Constants")]),t._v(" "),a("h2",{attrs:{id:"difference-between-function-and-method"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#difference-between-function-and-method"}},[t._v("#")]),t._v(" Difference between "),a("strong",[t._v("FUNCTION")]),t._v(" and "),a("strong",[t._v("METHOD")])]),t._v(" "),a("p",[a("code",[t._v("__FUNCTION__")]),t._v(" returns only the name of the function whereas "),a("code",[t._v("__METHOD__")]),t._v(" returns the name of the class along with the name of the function:")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token php language-php"}},[a("span",{pre:!0,attrs:{class:"token delimiter important"}},[t._v("")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("doit")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Outputs: doit")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$obj")]),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("->")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("doitagain")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Outputs: trick::doitagain")]),t._v("\n\n")])])])]),a("h2",{attrs:{id:"difference-between-class-get-class-and-get-called-class"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#difference-between-class-get-class-and-get-called-class"}},[t._v("#")]),t._v(" Difference between "),a("strong",[t._v("CLASS")]),t._v(", get_class() and get_called_class()")]),t._v(" "),a("p",[a("code",[t._v("__CLASS__")]),t._v(" magic constant returns the same result as "),a("code",[t._v("get_class()")]),t._v(" function called without parameters and they both return the name of the class where it was defined (i.e. where you wrote the function call/constant name ).")]),t._v(" "),a("p",[t._v("In contrast, "),a("code",[t._v("get_class($this)")]),t._v(" and "),a("code",[t._v("get_called_class()")]),t._v(" functions call, will both return the name of the actual class which was instantiated:")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token php language-php"}},[a("span",{pre:!0,attrs:{class:"token delimiter important"}},[t._v("")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("say")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Output:")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// __CLASS__ value: Definition_Class")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// get_called_class() value: Actual_Class")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// get_class($this) value: Actual_Class")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// get_class() value: Definition_Class")]),t._v("\n\n")])])])]),a("h2",{attrs:{id:"file-directory-constants"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#file-directory-constants"}},[t._v("#")]),t._v(" File & Directory Constants")]),t._v(" "),a("h3",{attrs:{id:"current-file"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#current-file"}},[t._v("#")]),t._v(" Current file")]),t._v(" "),a("p",[t._v("You can get the name of the current PHP file (with the absolute path) using the "),a("code",[t._v("__FILE__")]),t._v(" magic constant. This is most often used as a logging/debugging technique.")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("echo")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"We are in the file:"')]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("__FILE__")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"\\n"')]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),a("h3",{attrs:{id:"current-directory"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#current-directory"}},[t._v("#")]),t._v(" Current directory")]),t._v(" "),a("p",[t._v("To get the absolute path to the directory where the current file is located use the "),a("code",[t._v("__DIR__")]),t._v(" magic constant.")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("echo")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"Our script is located in the:"')]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("__DIR__")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"\\n"')]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),a("p",[t._v("To get the absolute path to the directory where the current file is located, use "),a("code",[t._v("dirname(__FILE__)")]),t._v(".")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("echo")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"Our script is located in the:"')]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("dirname")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("__FILE__")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string double-quoted-string"}},[t._v('"\\n"')]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),a("p",[t._v("Getting current directory is often used by PHP frameworks to set a base directory:")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// index.php of the framework")]),t._v("\n\n"),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("define")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("BASEDIR")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("__DIR__")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// using magic constant to define normal constant")]),t._v("\n\n")])])]),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// somefile.php looks for views:")]),t._v("\n\n"),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$view")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string single-quoted-string"}},[t._v("'page'")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$viewFile")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("BASEDIR")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string single-quoted-string"}},[t._v("'/views/'")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$view")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),a("h3",{attrs:{id:"separators"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#separators"}},[t._v("#")]),t._v(" Separators")]),t._v(" "),a("blockquote"),t._v(" "),a("p",[t._v("Windows system perfectly understands the `/` in paths so the\n`DIRECTORY_SEPARATOR` is used mainly when parsing paths.")]),t._v(" "),a("p",[t._v("Besides magic constants PHP also adds some fixed constants for working with paths:")]),t._v(" "),a("li",[t._v("`DIRECTORY_SEPARATOR` constant for separating directories in a path. Takes value `/` on *nix, and `\\` on Windows.\nThe example with views can be rewritten with:")]),t._v(" "),a("div",{staticClass:"language-php extra-class"},[a("pre",{pre:!0,attrs:{class:"language-php"}},[a("code",[a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$view")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token string single-quoted-string"}},[t._v("'page'")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$viewFile")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("BASEDIR")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("DIRECTORY_SEPARATOR")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),a("span",{pre:!0,attrs:{class:"token string single-quoted-string"}},[t._v("'views'")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token constant"}},[t._v("DIRECTORY_SEPARATOR")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v(".")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token variable"}},[t._v("$view")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),a("ul",[a("li",[t._v("Rarely used "),a("code",[t._v("PATH_SEPARATOR")]),t._v(" constant for separating paths in the "),a("code",[t._v("$PATH")]),t._v(" environment variable. It is "),a("code",[t._v(";")]),t._v(" on Windows, "),a("code",[t._v(":")]),t._v(" otherwise")])]),t._v(" "),a("h4",{attrs:{id:"remarks"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#remarks"}},[t._v("#")]),t._v(" Remarks")]),t._v(" "),a("p",[t._v("Magic constants are distinguished by their "),a("code",[t._v("__CONSTANTNAME__")]),t._v(" form.")]),t._v(" "),a("p",[t._v("There are currently eight magical constants that change depending on where they are used. For example, the value of "),a("code",[t._v("__LINE__")]),t._v("depends on the line that it's used on in your script.")]),t._v(" "),a("p",[t._v("These special constants are case-insensitive and are as follows:")]),t._v(" "),a("table",[a("thead",[a("tr",[a("th",[t._v("Name")]),t._v(" "),a("th",[t._v("Description")])])]),t._v(" "),a("tbody",[a("tr",[a("td",[a("code",[t._v("__LINE__")])]),t._v(" "),a("td",[t._v("The current line number of the file.")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__FILE__")])]),t._v(" "),a("td",[t._v("The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__DIR__")])]),t._v(" "),a("td",[t._v("The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to "),a("code",[t._v("dirname(__FILE__)")]),t._v(". This directory name does not have a trailing slash unless it is the root directory.")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__FUNCTION__")])]),t._v(" "),a("td",[t._v("The current function name")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__CLASS__")])]),t._v(" "),a("td",[t._v("The class name. The class name includes the namespace it was declared in (e.g. "),a("code",[t._v("Foo\\Bar")]),t._v("). When used in a trait method, "),a("code",[t._v("__CLASS__")]),t._v(" is the name of the class the trait is used in.")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__TRAIT__")])]),t._v(" "),a("td",[t._v("The trait name. The trait name includes the namespace it was declared in (e.g. "),a("code",[t._v("Foo\\Bar")]),t._v(").")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__METHOD__")])]),t._v(" "),a("td",[t._v("The class method name.")])]),t._v(" "),a("tr",[a("td",[a("code",[t._v("__NAMESPACE__")])]),t._v(" "),a("td",[t._v("The name of the current namespace.")])])])]),t._v(" "),a("p",[t._v("Most common use case for these constants is debugging and logging")])])}),[],!1,null,null,null);s.default=n.exports}}]);