搜索文档
首页
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); } } //根据现有的类定义一个新的类 class Square extends Rectangle { //方法来测试矩形是否也是正方形 public function isSquare(){ if($this->length == $this->width){ return true; // 正方形 } else{ return false; //不是正方形 } } } //从Square类创建一个新对象 $obj = new Square; // 设置对象属性值 $obj->length = 20; $obj->width = 20; // 调用对象方法 if($obj->isSquare()){ echo "正方形的面积是"; } else{ echo "矩形的面积是"; }; echo $obj->getArea(); ?>
运行结果