saplingPro
saplingPro

Reputation: 21329

why does the class_exists function return false?

In the following script I check the class_exists function. What is the scope of this function ? It returns false for this script when I test for this class.

<?php
namespace my;
class Tester {      
    public function check() {
        $classname = 'Tester';
        if(class_exists($classname)) {
            echo "class exists ! <br />";
        } else {
            echo "class doesn't exist ! <br />";
        }
    }
}   

$obj = new Tester();
$obj->check();

Output : class doesn't exist

Upvotes: 3

Views: 6526

Answers (1)

nickb
nickb

Reputation: 59709

Tester isn't in the global namespace. It's in the my namespace.

Both of these will work:

$classname = '\my\Tester';
$classname = 'my\Tester';

Upvotes: 6

Related Questions