搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
服务端开发
PHP 教程
PHP 高级教程
源代码
清空
点击运行
<?php //包含类定义 class Rectangle { //声明属性 public $length = 0; public $width = 0; //求周长的方法 public function getPerimeter(){ return (2 * ($this->length + $this->width)); } //求面积的方法 public function getArea(){ return ($this->length * $this->width); } } //从Rectangle类创建一个新对象 $obj = new Rectangle; //获取对象属性值 echo $obj->length; // 输出: 0 echo $obj->width; // 输出: 0 //设置对象属性值 $obj->length = 30; $obj->width = 20; //再次读取对象属性值以显示更改 echo $obj->length; // 输出: 30 echo $obj->width; // 输出: 20 //调用对象方法 echo $obj->getPerimeter(); // 输出: 100 echo $obj->getArea(); // 输出: 600 ?>
运行结果