Skip to content

Core\Ldap\Dn::parent

$dn->parent(): ?Core\Ldap\Dn

Returns the DN one level up, without its first part. For CN=Ann,OU=Staff,DC=example,DC=test the result is OU=Staff,DC=example,DC=test.

Use it to find the group or the unit that an entry is in.

Good to know: a DN with only one part has no parent, and the result is null.

Return value

Returns ?Core\Ldap\Dn. The parent DN, or null when this DN has only one part.

Examples

One level up

<?nvs
// Shows how to find the unit that a person's entry is in.
var $person = Core\Ldap\Dn::parse('CN=Ann,OU=Staff,DC=example,DC=test');
// This prints OU=Staff,DC=example,DC=test.
echo $person->parent()?->toString() ?? 'none', "\n";
Output
OU=Staff,DC=example,DC=test

Walk to the top

<?nvs
// Shows how to go up a DN one level at a time, until there is no parent.
Core\Ldap\Dn $at = Core\Ldap\Dn::parse('CN=Ann,OU=Staff,DC=example,DC=test');
// This prints four lines, and the last one is DC=test.
while (true) {
echo $at->toString(), "\n";
var $up = $at->parent();
if ($up == null) {
break;
} else {
$at = $up;
}
}
Output
CN=Ann,OU=Staff,DC=example,DC=test
OU=Staff,DC=example,DC=test
DC=example,DC=test
DC=test

No parent

<?nvs
// Shows the result for a DN with only one part.
var $top = Core\Ldap\Dn::of('DC', 'test');
// The result is `null`, so this prints no parent.
if ($top->parent() == null) {
echo "no parent\n";
}
Output
no parent