Bill
Bill

Reputation: 5668

How to access a class in a different namespace from another non namespaced class

I have a class, that doesn't have a namespace.... I have another class that does have a name space... If I try to access the namespaced class from the non namespace, it can't find it. How do I tell my class to access the other class in the new namespace with PHP?

Thanks!

Upvotes: 0

Views: 142

Answers (1)

rjz
rjz

Reputation: 16510

Precede the name of the namespace-less class with a \. For instance, to create a DateTime within a namespace, you could use:

<?php namespace bar;

class Foo {
  function hello() {
    echo new \DateTime();
  }
}

Conversely, you can access the namespaced class by preceding the class with the namespace and a \:

<?php
  $f = new \bar\Foo();
  $f->hello();

Upvotes: 1

Related Questions