0% found this document useful (0 votes)
6 views7 pages

PHP PR 8,9 (22203C0007)

The document outlines two experiments for a Computer Engineering course, focusing on object-oriented programming in PHP. Experiment 8 involves creating a Student class and a Result class to calculate test scores, while Experiment 9 involves collecting user profile information, serializing it, and storing it in a file. Additionally, the document explains serialization in PHP, highlighting its importance for data storage and transfer.

Uploaded by

adityadarekar919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

PHP PR 8,9 (22203C0007)

The document outlines two experiments for a Computer Engineering course, focusing on object-oriented programming in PHP. Experiment 8 involves creating a Student class and a Result class to calculate test scores, while Experiment 9 involves collecting user profile information, serializing it, and storing it in a file. Additionally, the document explains serialization in PHP, highlighting its importance for data storage and transfer.

Uploaded by

adityadarekar919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF COMPUTER ENGINEERING

Subject: ETI Subject Code:22618


Semester: 6th Semester Course: Computer Engineering
Laboratory No: L004 Name of Subject Teacher: Prof. Sneha
Patange
Name of Student: Aditya Darekar Roll Id: 22203C0007

Experiment No: 8&9

Experiment no-8
• Create student class with Roll number, class and div C. Then class
Test which include classtest1 and class test2 data in the form of
Associative array.
You will read it and Assign ir to class data members.
Third, result class will calculate percentage and average of Test
1 and Test 2
Solution:
<?php
// Student class
class Student {
public $rollNo;
public $class;
public $division;

public function __construct($rollNo, $class, $division) {


$this->rollNo = $rollNo;
$this->class = $class;
$this->division = $division;
}

public function displayStudent() {


echo "Roll No: " . $this->rollNo . "\n";
echo "Class: " . $this->class . "\n";
echo "Division: " . $this->division . "\n";
}
}

// Test class
class Test {
public $marks = []; // Associative array
public function setMarks($test1, $test2) {
$this->marks["Test1"] = $test1;
$this->marks["Test2"] = $test2;
}

public function getMarks() {


return $this->marks;
}
}

// Result class (inherits from Student and uses Test class as a property)
class Result extends Student {
private $test; // Object of Test class

public function __construct($rollNo, $class, $division) {


parent::__construct($rollNo, $class, $division);
$this->test = new Test(); // Create a Test object
}

public function setMarks($test1, $test2) {


$this->test->setMarks($test1, $test2);
}

public function calculateResult() {


$marks = $this->test->getMarks();
$totalMarks = array_sum($marks);
$average = $totalMarks / count($marks);
$percentage = ($totalMarks / (100 * count($marks))) * 100; // Assuming each test is out of 100

echo "Total Marks: " . $totalMarks . "\n";


echo "Average Marks: " . number_format($average, 2) . "\n";
echo "Percentage: " . number_format($percentage, 2) . "%\n";
}
}

// Create student object and assign test marks


$student1 = new Result(101, "10th", "C");
$student1->setMarks(85, 90);

// Display student details and results


$student1->displayStudent();
$student1->calculateResult();
?>

Output:

Experiment no-9
• Collect User Profile Information in Class
(Name,Email,Age,Contact Number)
Store it in file
Use Serialize and Unserialize
Solution:
<?php
class User {
public $name, $email, $age,
$contact;

function __construct($name,
$email, $age, $contact) {
$this->name = $name;
$this->email = $email;
$this->age = $age;
$this->contact = $contact;

$data = serialize([
'name' => $this->name,
'email' => $this->email,
'age' => $this->age,
'contact' => $this->contact
]) . "\n";

if
(!file_put_contents("data.txt",
$data, FILE_APPEND)) {
throw new
Exception("Failed to write to
data.txt");
}
}

function readData($inputFile,
$outputFile) {
if (!file_exists($inputFile)) {
throw new
Exception("Input file not
found");
}

$lines = file($inputFile,
FILE_IGNORE_NEW_LINES |
FILE_SKIP_EMPTY_LINES);
$output = "";

foreach ($lines as $line) {


$user = unserialize($line);
$text = "Name: " .
$user['name'] . ", Email: " .
$user['email'] .
", Age: " . $user['age']
. ", Contact: " . $user['contact'] .
"\n";
$output .= $text;
}
if
(!file_put_contents($outputFile,
$output)) {
throw new
Exception("Failed to write to
output file");
}
}
}

try {
$user = new User("xyz",
"[email protected]", 18,
"7400320XXX");
$user->readData("data.txt",
"output.txt");
echo "Successfully processed
the data!";
} catch (Exception $e) {
echo "Error: " . $e-
>getMessage();
}
?>

Output:
• What is Serialization
Use of Serialization and why it's important with respect to PHP
Solution:
Serialization in PHP is the process of converting data, such as arrays or
objects, into a string format so it can be stored or transferred easily. It is useful
for
saving data in files, databases, or sessions and for sending data
between systems. The serialize() function converts data into a
storable string, and unserialize() restores it
Code:
<?php
// Define a Student class
class Student {
public $name;
public $rollNo;

public function __construct($name, $rollNo) {


$this->name = $name;
$this->rollNo = $rollNo;
}
}

// Create an object of Student


$student1 = new Student("John Doe", 101);

// Serialize the object (convert object to string)


$serializedData = serialize($student1);
echo "Serialized Data: " . $serializedData . "\n";

// Deserialize the object (convert string back to object)


$unserializedObject = unserialize($serializedData);
echo "Unserialized Data: Name = " . $unserializedObject->name . ", Roll No =
" . $unserializedObject->rollNo . "\n";
?>
Output:

You might also like