การสร้าง Constructor ของ Class
รายละเอียด
//File: Human.php
Code:
<?php
class Human
{
var $name ;
var $height;
var $weight;
function Human($aName,$aHeight,$aWeight) // Constructor คือ function ที่มีชื่อเดียวกับ class ทำหน้าที่ ในการกำหนดค่าเริ่มต้น
{
$this->name = $aName;
$this->height = $aHeight;
$this->weight = $aWeight;
}
function setName($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
function setHeight($height)
{
$this->height = $height;
}
function getHeight()
{
return $this->height;
}
function setWeight($weight)
{
$this->weight = $weight;
}
function getWeight()
{
return $this->weight;
}
}
?>
// File: TestClient.php
<?
require("Human.php");
$human = new Human("Doing",160,60);
echo "Name: ".$human->getName();
echo "<br>Height: ".$human->getHeight();
echo "<br>Weight: ".$human->getWeight();
?>
Code:
Result
----------
Name: Doing
Height: 160
Weight: 60
Ref: https://sites.google.com/site/oopinphp/