wireClassNamespace() function

Get namespace for given class

Available since version 3.0.150.

Example

echo wireClassNamespace('Page'); // returns: "\ProcessWire\"
echo wireClassNamespace('DirectoryIterator'); // returns: "\"
echo wireClassNamespace('UnknownClass'); // returns "" (blank)

// Specify true for 2nd argument to make it include class name
echo wireClassNamespace('Page', true); // outputs: \ProcessWire\Page

// Specify true for 3rd argument to find all matching classes
// and return array if more than 1 matches (or string if just 1):
$val = wireClassNamespace('Foo', true, true);
if(is_array($val)) {
  // 2+ classes found, so array value is returned
  // $val: [ '\Bar\Foo', '\Foo', '\Baz\Foo' ]
} else {
  // string value is returned when only one class matches
  // $val: '\Bar\Foo'
}

Usage

// basic usage
$string = wireClassNamespace($className);

// usage with all arguments
$string = wireClassNamespace($className, bool $withClass = false, bool $strict = false);

Arguments

NameType(s)Description
classNamestring, object
withClass (optional)bool

Include class name in returned namespace? (default=false)

strict (optional)bool

Return array of namespaces if multiple match? (default=false)

Return value

string array

Returns one of the following:

  • String of \Namespace\ (leading+trailing backslashes) if namespace found.
  • String of \ if class in root namespace.
  • Blank string if unable to find namespace for class.
  • Array of namespaces only if $strict option is true AND multiple namespaces were found for class.
  • If the $withClass option is true, then return value(s) have class, i.e. \Namespace\ClassName.

Functions methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #547
    In the 547th issue of ProcessWire Weekly we're going to check out the latest core updates, introduce a couple of new modules, and more. Read on!
    Weekly.pw / 2 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

I just love the easy and intuitive ProcessWire API. ProcessWire rocks!” —Jens Martsch, Web developer