Search code examples
phpnamespacesphp-stream-wrappers

Can I use a namespaced class with stream_wrapper_register?


I have a class VarStream that I would like to register like

stream_wrapper_register('var', 'VarStream');

Our classes are always namespaced, so I've tried

stream_wrapper_register('var', '\OurSpace\VarStream');

with no luck.

Can I use namespacing when registering a stream wrapper?


Solution

  • Yes you can. You need to provide the FQCN (Fully-Qualified-Class-Name) of the stream wrapper class.

    You might think that in the code you provided you have it already:

    stream_wrapper_register('var', '\OurSpace\VarStream');
    

    But that is not the case as the FQCN never starts with the back-slash "\".

    Instead use (just) the class-name of that class:

    stream_wrapper_register('var', 'OurSpace\VarStream');
    

    Normally the leading backslash is not necessary. In this case even, it prevents proper use. It is not part of the (fully-qualified) class-name. This is normally always the case when you pass class-names as string parameters.