<?phpclass Car{var $color;function __construct($color=‘green‘){$this->color=$color; ???}function what_color(){return $this->color; ???}}function print_var($obj){ ???foreach(get_object_vars($obj) as $props=>$val){ ????echo "$props=$val"; ???????} ???}$myCar=new Car();print_var($myCar);//echo $myCar->what_color();?>
在php中定义类是,应注意定义属性使用var ,引用属性和方法使用$this,注意前面需要添加$,另外,在新版本的PHP中,使用了__construct函数,是两个下划线。
注意:get_object_vars方法:返回由对象属性组成的关联数组
php 中定义类
原文地址:https://www.cnblogs.com/qietu/p/8505047.html