PHP - 检查命名空间内是否存在全局类 [英] PHP - Check if global class exists inside namespace

查看:1510
本文介绍了PHP - 检查命名空间内是否存在全局类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你在另一个类的命名空间内,你如何检查一个全局类是否存在class_exists?例如:

How can you check if a global class exists with class_exists if you are inside the namespace of another class? For example:

<?php
namespace Rvdv\Test\Example;

class ExampleClass
{
    public function testNamespace()
    {
        // This says that it doesn't exists :(
        print class_exists('\\Test');
    }
}

其中类是一个全局定义的类。

Where class is a global defined class.

推荐答案

你不得不在你的其他问题,测试不在这个范围内声明(你忘了include吗?)我测试了这样:

You've had to screw up something in your other issues - most likely the class test is not declared in this scope (did you forget include?). I tested this thusly:

phpcltest2.php:

phpcltest2.php:

<?php
class Test { }


$ b b

phpcltest.php:

phpcltest.php:

<?php
namespace Rvdv\Test\Example;
require 'phpcltest2.php';

class ExampleClass
{
    public function testNamespace()
    {
    print class_exists('\\Test');
    }
}

$nc = new ExampleClass();
$nc->testNamespace();

结果是预期的:它打印出1。因此,请检查您的包含路径。

The results were expected: it prints out "1". So check your include paths.

这篇关于PHP - 检查命名空间内是否存在全局类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆