How to use namespace and use in PHP? -


i have file index.php in directory main;

there directory helpers inside main class helper.

i tried inject class helpers\helper in index.php as:

<?  namespace program;  use helpers\helper;   class index {      public function __construct()     {         $class = new helper();     }  } 

but not work.

how use namespace , use in php?

with description, directory structure should similar this:

    main*         -- index.php         |         helpers*                --helper.php 

if going book regards psr-4 standards, class definitions similar ones shown below:

index.php

    <?php         // file-name: index.php.         // located inside "main" directory         // presumed @ root of app. directory          namespace main;            //<== notice main here namespace...          use main\helpers\helper;   //<== import helper class use here             // if not using auto-loading mechanism, may have          // manually import "helper" class using either include or require         require_once __dir__ . "/helpers/helper.php";          class index {              public function __construct(){                 $class = new helper();             }          } 

helper.php

    <?php         // file name helper.php.         // located inside "main/helpers" directory           namespace main\helpers;     //<== notice main\helpers here namespace...           class helper {              public function __construct(){                 // initialisation code             }          } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -