一切皆对象,类也是对象,类来自于元类type,如果一个类没有声明自己的元类,默认它就是元类. 即类是元类的实例,通过type(类)会显示type,而实例来自于类. 类有两个属性,数据属性和函数属性,下面是一个创建类和实例化对象的例子 class animal: 'This is class for animal' #类的说明 type='animal' def __init__(self,name,sex,leg): self.name = name self.sex = sex self.le
在框架开发,模块化开发等场合,我们可能有一种需求,那就是在PHP运行时动态实例化对象. 什么是动态实例化对象呢?我们先来看一下PHP有一种变量函数(可变函数)的概念,例如如下代码: function foo() { echo 'This is the foo function'; } $bar = 'foo'; $bar(); 运行上述代码将会输出“This is the foo function”.具体请参考PHP手册:可变函数.当然,如果需要动态调用的话,那么就使用call_user_fun