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 #514
    In the 514th issue of ProcessWire Weekly we'll check out the latest blog post from Ryan, introduce two new third party modules — Page List Versions Counter and Fieldtype Fieldset Panel — and more. Read on!
    Weekly.pw / 16 March 2024
  • Invoices Site Profile
    The new invoices site profile is a free invoicing application developed in ProcessWire. It enables you to create invoices, record payments to them, email invoices to clients, print invoices, and more. This post covers all the details.
    Blog / 15 March 2024
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits