How to Use Class using PHP Programming Language
This is the simplest way to convert from class diagram to PHP program.
Class Diagram:
Student |
StudentNo: String Name: String Address: String |
setStudentNo() getStudentNo() setName() getName() setAddress() getAddress() |
PHP program:
<?php
//create class Student with 3 attributes and the operations to manage them
class Student{
var $StudentNo;
var $Name;
var $Address;
//the operation to set the StudentNo attribute
function setStudentNo($newStudentNo){
$this->StudentNo = $newStudentNo;
}
//the operation to get the value of StudentNo
function getStudentNo(){
return $this->StudentNo;
}
//the operation to set Name
function setName($newName){
$this->Name = $newName;
}
//the operation to get student name
function getName(){
return $this->Name;
}
//the operation to set address
function setAddress($newAddress){
$this->Address = $newAddress;
}
//the operation to get the address
function getAddress(){
return $this->Address;
}
}
//implemented the student class
$James = new Student(); //create the Student object
//set the attributes values of object James
$James->setStudentNo("007");
$James->setName("James Bono");
$James->setAddress("Sumedang");
//display data to the screen
echo "Student No : "; echo $James->getStudentNo();
echo "<br>";
echo "Name : "; echo $James->getName();
echo "<br>";
echo "Address : "; echo $James->getAddress();
?>
Output:
ShadowOfBdg© - All rights reserved.
No comments:
Post a Comment