Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does the class_exists function return false?

Tags:

php

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

like image 473
saplingPro Avatar asked Apr 17 '26 06:04

saplingPro


1 Answers

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

Both of these will work:

$classname = '\my\Tester';
$classname = 'my\Tester';
like image 132
nickb Avatar answered Apr 18 '26 20:04

nickb



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!