百科狗-知识改变命运!
--

get_declared_classes() - php 类对象函数

乐乐1年前 (2023-11-21)阅读数 22#技术干货
文章标签数组

get_declared_classes()

(PHP 4, PHP 5, PHP 7)

返回由已定义类的名字所组成的数组

说明

get_declared_classes(void): array

返回由当前脚本中已定义类的名字组成的数组。

返回值

返回由当前脚本中已定义类的名字组成的数组。

Note:

需要注意的是额外类的出现依赖于你已编译到 PHP 中的库。这意味着你不能使用这些类名定义自己的类。在附录的预定义类部分有预定义类的列表。

范例

Example #1get_declared_classes()例子

get_declared_classes() - php 类对象函数

以上例程的输出类似于:

Array
(
    [0] => stdClass
    [1] => __PHP_Incomplete_Class
    [2] => Directory
)

参见

  • class_exists() 检查类是否已定义
  • get_declared_interfaces() 返回一个数组包含所有已声明的接口
  • get_defined_functions() 返回所有已定义函数的数组
The array returned by this function will be in the order the classes were defined / included / required and this order does not appear to change.
For example:

Output:
Array
( 
  [0] => stdClass
  [1] .... other defined classes....
  [10] => classone
  [11] => classtwo
 )
and...
Array
( 
  [0] => stdClass
  [1] .... other defined classes....
  [10] => classone
  [11] => classtwo
  [12] => classthree
  [13] => classfour
)
Regarding note of 3-21:

This function could also be used to determine the names of classes defined in a particular file by calling it before and after include. It's hardly a pointless function.
get-declared-classes makes no sense at all, if u maybe, later for production, merge class files in one package file. 
lets say: package.php
print_r(get_declared_classes());
class declaredHere { }
print_r(get_declared_classes());
so in this case, the declaredHerr class is defined at the first call of print_r();
because PHP-complier runs a hole file and declare Stuff before running the code.
But (Lovely PHP):
print_r(get_declared_classes());
if(true){
class declaredHere { }
}
print_r(get_declared_classes());
Will print the declaredHere class only in the second print_r.
Its not a Bug it a...
Summary:
 * in PHP 5.1 class names have case preserved
 * contrary, in PHP 4.4 class names are downcased, withe exception of a few build-in ones
The get_declared_classes() funcition returns the list of names with case preserved, as of PHP 5.1 series (prolly 5.0 too, but i have no way to test it right now). Since PHP generally is caseless in regard to names of classes, this may come at a surprise. Also, this could potentially break older code asssuming downcased list.
Take extra care when checking for existence of a class. Following example is, potentially, error prone: 
A sure-fire (while slower) way would be to iterate over the array and normalize case to, say, lower:

Optimization of the above snippet is left as a simple excercise to the reader ;)
-- dexen deVries
classes can't be unloaded. probably not very practical to implement that in a future version. I wouldn't go out of my way to do it if I were zend. you're better off finding a workaround. it's better programming technique to find a way around having to do that anyway.
http://www.zend.com/zend/week/week223.php#Heading10
you cannot remove them. they are "defined", which happens when the class is being loaded from the parser. you just deleted an instance of a class.
This function considers only classes and subclasses. Not subsubclasses.
In fact I have code that provides an abstract class and then classes using this abstract class. Further I have subclasses to my concrete classes - which is why my subclasses are not listed within the returned array.
In PHP5, you don't get declared interfaces by calling this function!!!
To get interfaces you should use get_declared_interfaces(). However, to check if an interface is already defined, you should use class_exists()! This is strange, but PHP team does not think so.
those above comments are too old.
now, whatever the order is, the output will be the same:

will output the same result.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)