If PHPExcel doesn't use namespaces, then it lives in the root namespace \.

You just use it like you would any old non-namespaced code.

I think the confusion comes from the PHP manual interchanging aliasing and importing. The use statement only creates an alias. It does not do any importing per se, but rather creates a named link to another namespace, class or interface.

So that said, you can still use the "use" statement to "import" the PHPExcel_IOFactory into your current scope:

namespace MyApp;

use \PHPExcel_IOFactory as ExcelFactory;   <- alias is created

new ExcelFactory (); // \PHPExcel_IOFactory <- autoloading happens here


As a further example:
namespace MyApp {
    class PHPExcel {} // \MyApp\PHPExcel
}

class PHPExcel {} // \PHPExcel

-- then somewhere else ---
namespace MyApp{
new PHPExcel; // \MyApp\PHPExcel is loaded (first class defined above)
new \PHPExcel; // \PHPExcel (second class defined above)
}

namespace MyApp{

use \PHPExcel;
// must use leading \ otherwise it will alias the one in the MyApp namespace

new PHPExcel; // \PHPExcel;
new \MyApp\PHPExcel; // \MyApp\PHPExcel

}


Cheers,
David

On 14/03/13 07:19, Brad Waite wrote:
Thanks for the reply, Marco.

While I haven't been that fortunate to deal with a lot of modern code that uses 
namesapces, I'm familiar with the
concepts. I re-read the manual before posting just in case I was missing 
something simple and I didn't think that I was.

Here's my understanding, please correct me if I'm wrong:

The static method createReaderForFile() is in the class PHPExcel_IOFactory, 
which lives within the PHPExcel directory.
Since the library doesn't use namespaces, underscores denote a subdirectory - 
just like in ZF1.

Give the fact that the PHPExcel class lives in 
/vendor/phpoffice/phpexcel/Classes and the autoload_namespaces.php maps
'PHPExcel' to that directory, I would expect that by adding the 'use' statement 
in another file would permit access to
the method without any namespace prefixes or separators. I can see that if I 
omitted the use, then without the leading
'\', PHP would think that I'm referring to a class that's in the current 
namespace, but that's not the case.

Everything I've ready seems to back up that theory, but since it's not working 
as expected, I'm still missing something.

-Brad

P.S. That 2 min video is great - I'll have to pass it on to colleagues who are 
namespace-ignorant.

On 3/13/2013 12:16 PM, Marco Pivetta wrote:
Correct. That's basic PHP namespaces knowledge though.

Here's a couple of resources you should read up before dealing with namespaces:

The manual (obviously): http://php.net/manual/en/language.namespaces.php
Namespaces in 120 seconds: http://www.youtube.com/watch?v=d1kDT8YFAhI

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to