Skip to content

Instantly share code, notes, and snippets.

@7kaji
Created April 20, 2014 13:13
Show Gist options
  • Save 7kaji/11113893 to your computer and use it in GitHub Desktop.
Save 7kaji/11113893 to your computer and use it in GitHub Desktop.
Sample code.
<?php
class Triangle {
public function __construct($width, $height){
$this->width = $width;
$this->height = $height;
}
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 triangle(){return $this -> width * $this -> height / 2;}
}
class Car {
private $name;
private $color;
// setter
public function __set($key, $value){
$this->$key = $value;
}
// getter
public function __get($key){
return $this->$key;
}
}
$obj = new Triangle(5,10);
echo("width is {$obj -> get_width()} cm.\n");
echo("height is ". $obj -> get_height(). "cm."."\n");
echo "Area is {$obj -> triangle()} cm2.\n";
// privateエラー
// echo("height : ".$obj -> height."\n");
$fit = new Car();
$fit->name = "fit\n";
$fit->color = "white\n";
echo $fit->name;
echo $fit->color;
$fit->name = "aquos\n";
$fit->color = "black\n";
echo $fit->name;
echo $fit->color;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment