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 #549
    In the 549th issue of ProcessWire Weekly we’re going to check out the latest core updates, highlight one older yet still very relevant third party module, and more. Read on!
    Weekly.pw / 17 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

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.