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
Post a Comment