Php – Basic OO with PHP

namespacesPHP

without telling me to buy a book, would anyone be interested in answering the following question?

if i am in a namespace with a class named foo. and i wanted to build another class called bar. how would i proceed to make foo, aware of bar and vice versa? at what cost? keep in mind that there could potentially be a whole microcosm of useful classes

Best Answer

No book, but see the namespace documentation

If your classes are in different namespaces:

<?php
namespace MyProject;

class Bar { /* ... */ }

namespace AnotherProject;

class Foo{ /* ... */ 
   function test() {
      $x = new \MyProject\Bar();
   }
}
?>

If the classes are in the same namespace, it's like no namespace at all.