戻る      call.php       Triangle.class.php      

《PHP言語 /call.php》

<html>
<head>
<title>オブジェクト指向入門</title>
</head>
<body>
<h1 style="background:#cccccc">オブジェクト指向入門</h1>
<?php
function __autoload($name){
require_once($name.".class.php");
}
$objCls=new Triangle();
$objCls->set_width(10);
$objCls->set_height(20);
print("三角形の面積:".$objCls->get_area()."<br />");
print("三角形の面積:".Triangle::calculate_area(5,15));
?>
</body>
</html>


《PHP言語 /Triangle.class.php》

<?php
class Triangle {
private $width;
private $height;
public function __construct(){
$this->width=1;
$this->height=1;
}
public function __destruct(){ /* デストラクタ */ }
public function get_width() {return $this->width;}
public function get_height(){return $this->height;}
public function set_width($width){
if(is_numeric($width)){$this->width=$width;}
}
public function set_height($height){
if(is_numeric($height)){$this->height=$height;}
}
public function get_area(){
return $this->get_width() * $this->get_height() /2;
}
public static function calculate_area($width,$height){
return $width * $height /2;
}
}
?>