{"id":1487,"date":"2020-12-26T11:10:07","date_gmt":"2020-12-26T11:10:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/use-class-in-root-script-magento-2\/"},"modified":"2025-05-21T17:23:45","modified_gmt":"2025-05-21T11:53:45","slug":"use-class-in-root-script-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-class-in-root-script-magento-2\/","title":{"rendered":"How To Use Class in Root Script in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Root scripts in Magento 2 quickens the process of retrieving the data like getting the server time and store time.&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-root-script-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Creating a root script in Magento 2<\/a>&nbsp;helps developer to save the time. However, a root script has a problem too. In root scripts, we cannot take the complete advantage of IDE auto-completion which boosts the productivity of the developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By leveraging the class in a root script, the IDE starts supporting auto-completion facility. Hence, it\u2019s a better choice for developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post explains the programmatic solution to&nbsp;<strong>use class in root script in Magento 2<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programmatic Solution to Use Class in Root Script in Magento 2<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace rootScript;\nuse Magento\\Framework\\App\\Bootstrap;\n\nrequire __DIR__ . '\/app\/bootstrap.php';\n\n$params = $_SERVER;\n\n$bootstrap = Bootstrap::create(BP, $params);\n\n$obj = $bootstrap->getObjectManager();\n\n$state = $obj->get('Magento\\Framework\\App\\State');\n$state->setAreaCode('frontend');\n\nClass rootClass {\n\n    \/**\n     * @var \\Magento\\Framework\\Stdlib\\DateTime\\DateTime\n     *\/\n    private $date;\n\n    public function __construct(\n        \\Magento\\Framework\\Stdlib\\DateTime\\DateTime $date\n    ){\n        $this->date = $date;\n    }\n\n    public function printTime()\n    {\n       echo $this->date->gmtDate();\n    }\n}\n\n$rootScript=$obj->get('\\rootScript\\rootClass');\n$rootScript->printTime();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Done! Here you go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Root scripts in Magento 2 quickens the process of retrieving the data like getting the server time and store time.&nbsp;Creating a root script in Magento&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1487","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1487"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1487\/revisions"}],"predecessor-version":[{"id":13903,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1487\/revisions\/13903"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}