Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Using imported class without namespace in class with namespace

I'm using a pear library that doesn't utilize namespaces and some composer based libraries which, obviously, do. I'd like to use namespaces in my own code, but I find that when I import classes from the pear library (non-namespaced) I can't reference them correctly.

<?php namespace Foo\Bar;

require_once "pearLib/Baz.php";

class MyClass extends Baz { ... }

PHP looks for the class Foo\Bar\Baz, giving me the message The Class 'Foo\Bar\DataType' not found. I simply want the non-namespaced Baz class which I just imported. Suggestions???

like image 742
Ian Walker-Sperber Avatar asked May 23 '26 03:05

Ian Walker-Sperber


1 Answers

To get it off the unanswered list:


Use a backslash before the class name like:

class MyClass extends \Baz
like image 154
cweiske Avatar answered May 24 '26 16:05

cweiske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!