Namcspaces and include()

[email protected]
Newsgroups php.general
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.