Mark Kasson
Mark Kasson

Reputation: 1710

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?

Upvotes: 1

Views: 761

Answers (2)

Alex.Default
Alex.Default

Reputation: 236

I've solved such problem with usage of ::class, which

allows for fully qualified class name resolution at compile

For example,

stream_wrapper_register("stream", self::class)

So you don't have to think about (back)slashes by yourself ;]

Upvotes: 1

hakre
hakre

Reputation: 197712

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.

Upvotes: 1

Related Questions