Define class to store point information in PHP

Description

The following code shows how to define class to store point information.

Example


<?php/*from w w w.java2s.  com*/
class Point{
  public $x;

  public $y;

  function __construct($x, $y){
    $this->x = $x;
    $this->y = $y;
  }

  function get_x(){
    return ($this->x);
  }

  function get_y(){
    return ($this->y);
  }

  function dist($p){
    return (sqrt(pow($this->x - $p->get_x() , 2) + pow($this->y - $p->get_y() , 2)));
  }
}

// Class ends here

$p1 = new Point(2, 3);
$p2 = new Point(3, 4);
echo $p1->dist($p2) , "\n";
$p2->x = 5;
echo $p1->dist($p2) , "\n";
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition