{"id":417,"date":"2024-03-21T14:28:57","date_gmt":"2024-03-21T13:28:57","guid":{"rendered":"https:\/\/solarxbike.se\/labbet\/code-snippets\/?p=417"},"modified":"2024-03-21T14:35:17","modified_gmt":"2024-03-21T13:35:17","slug":"included-files","status":"publish","type":"post","link":"https:\/\/solarxbike.se\/labbet\/code-snippets\/417\/included-files\/","title":{"rendered":"Included Files"},"content":{"rendered":"<pre><strong>K\u00e4lla<\/strong>: <a href=\"https:\/\/code.tutsplus.com\/how-to-include-and-require-files-and-templates-in-wordpress--wp-26419a\">Code tutor<\/a> | <a href=\"https:\/\/www.php.net\/manual\/en\/function.require-once.php\">php.net<\/a> |<\/pre>\n<hr \/>\n<p>When you include a file inside a parent file, the scope of variables defined in the included file will be the same as the variable scope at that particular line. However, any functions and classes that you have defined in the included file will have a global scope. Let&#8217;s understand it through an example<\/p>\n<ol>\n<li>create file inc.php<\/li>\n<li>Define to include &#8221;include.php in new file<\/li>\n<\/ol>\n<pre>&lt;?php\r\n\r\nrequire_once('inc.php'); \r\n\r\n?&gt;<\/pre>\n<hr \/>\n<p>&nbsp;<\/p>\n<h4>Path<\/h4>\n<p>Always use __DIR__ to define path relative to your current __FILE__.<br \/>\n(Or another setting that is originally based on __DIR__\/__FILE__)<\/p>\n<hr \/>\n<p>One of my typical example is:<\/p>\n<p><span class=\"default\">&lt;?php<br \/>\ndefine<\/span><span class=\"keyword\">(<\/span><span class=\"string\">&#8217;__ROOT__&#8217;<\/span><span class=\"keyword\">, <\/span><span class=\"default\">dirname<\/span><span class=\"keyword\">(<\/span><span class=\"default\">dirname<\/span><span class=\"keyword\">(<\/span><span class=\"default\">__FILE__<\/span><span class=\"keyword\">)));<br \/>\nrequire_once(<\/span><span class=\"default\">__ROOT__<\/span><span class=\"keyword\">.<\/span><span class=\"string\">&#8217;\/config.php&#8217;<\/span><span class=\"keyword\">);<br \/>\n<\/span><span class=\"default\">?&gt;<br \/>\n<\/span><br \/>\ninstead of:<br \/>\n<span class=\"default\">&lt;?php <\/span><span class=\"keyword\">require_once(<\/span><span class=\"string\">&#8217;\/var\/www\/public_html\/config.php&#8217;<\/span><span class=\"keyword\">); <\/span><span class=\"default\">?&gt;<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>K\u00e4lla: Code tutor | php.net | When you include a file inside a parent file, the scope of variables defined in the included file will be the same as the variable scope at that particular line. However, any functions and classes that you have defined in the included file will have a global scope. Let&#8217;s &#8230; <a title=\"Included Files\" class=\"read-more\" href=\"https:\/\/solarxbike.se\/labbet\/code-snippets\/417\/included-files\/\" aria-label=\"L\u00e4s mer om Included Files\">L\u00e4s mer<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-okontrollerad"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":7,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/posts\/417\/revisions\/424"}],"wp:attachment":[{"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solarxbike.se\/labbet\/code-snippets\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}