使用内置类在下面的内容中例如,一个新类使用预定义的 stdclass 作为基类。我们通过添加前缀 \来引用它来指定全局类
示例<?namespace testspace;class testclass extends \stdclass{ //}$obj=new testclass();$obj->name="raju";echo $obj->name;?>
包含的文件将默认使用全局命名空间。因此,要引用包含文件中的类,必须在前面加上\
示例#test1.php<?phpclass myclass{ function hello(){ echo "hello world";}}?>
此文件包含在另一个 php 脚本中,其类由 \ 引用
当此文件包含在另一个命名空间中时
示例#test2.php<?phpinclude 'test1.php';class testclass extends \myclass{function hello(){ echo "hello php"; }}$obj1=new \myclass();$obj1->hello();$obj2=new testclass();$obj2->hello();?>
输出这将打印以下输出
hello worldhello php
以上就是php访问全局类的详细内容。
