Using PHP namespaces on a subnamespace -
good day. i'm working on example understand namespces are, , limitations. have structure (windows):
d:\server\sistemas\tests\phpns\exec.php
d:\server\sistemas\tests\phpns\extra\call.php
d:\server\sistemas\tests\phpns\extra\exec.php --> (copy of other one)
d:\server\sistemas\tests\phpns\lib\base.php
code each one:
exec.php
<?php spl_autoload_register(); $a = new extra\call(); ?>
call.php
<?php namespace extra{ spl_autoload_register(); class call{ public function __construct(){ $inst = new \libs\base(); var_dump($inst); } } } ?>
base.php
<?php namespace libs{ class base{ public $dac; public $doc; public function __construct(){ } } } ?>
if work under scenario, totally ok. problem when execute extra\exec.php
extra\exec.php
<?php namespace extra{ spl_autoload_register(); $a = new call(); } ?>
it's supposed filesystem context of file that's why i'm using namespace not working. got error.
fatal error: spl_autoload(): class extra\call not loaded in d:\server\sistemas\tests\phpns\extra\exec.php on line 5
then remember php classes called trailing backslash (\iterator) tried too:
extra\exec.php
<?php spl_autoload_register(); $a = new \extra\call(); ?>
but got same result... doing wrong, , should if want use file in or in n-depth directory?
first off, it's not recommended use namespace
declarations blocks. it's preferred declare namespace
, go classes , place 1 class per file. how declare \extra\call
namespace extra; class call{ public function __construct(){ $inst = new \libs\base(); var_dump($inst); } }
you typically use spl_autoload_register();
name of callable function haven't provided functions act autoloader. autoload function should have sole argument name of class you're trying load. should find class file , include
or require
file. here's example from php docs
spl_autoload_register(function ($class) { include 'classes/' . $class . '.class.php'; });
Comments
Post a Comment