Namcspaces and include()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Folks:

I've been coding in PHP for almost two decades, but I've never used
namespaces in my code. Please verify or correct my understanding of how
namespaces work with include() or require().

In order for a namespace declared at the top of a file to actually be
of any use, the code file which defines the namespace originally must
be included somewhere in a file before the namespace declare. That is,

file1.php:
<?php
namespace \something
// some code which is used in other files

file2.php:
<?php
namespace \something
// the code which uses the namespaced code

file3.php:
<?php
include 'file1.php';
// include this before using the namespace code elsewhere 

In other words, the fact of declaring a namespace does not pull in that
code from elsewhere. You must do an include() or require() somewhere in
order to actually use the namespaced code.

I know, this sounds like a really dumb newbie question. Mea culpa.

Paul

-- 
Paul M. Foster
Personal Blog: http://noferblatz.com
Company Site: http://quillandmouse.com
Software Projects: https://gitlab.com/paulmfoster



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux